I trying to get the google maps auto complete textview , I have taken resource from http://wptrafficanalyzer.in/blog/android-autocompletetextview-with-google-places-autocomplete-api/
I have compiled app , app runs but auto complete is not working !
it is like normal textview !
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.wptrafficanalyzer.locationplacesautocomplete"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="in.wptrafficanalyzer.locationplacesautocomplete.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<in.wptrafficanalyzer.locationplacesautocomplete.CustomAutoCompleteTextView
android:id="#+id/atv_places"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:hint="#string/str_atv_places" />
</RelativeLayout>
jsonfile
package in.wptrafficanalyzer.locationplacesautocomplete;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class PlaceJSONParser {
/** Receives a JSONObject and returns a list */
public List<HashMap<String,String>> parse(JSONObject jObject){
JSONArray jPlaces = null;
try {
/** Retrieves all the elements in the 'places' array */
jPlaces = jObject.getJSONArray("predictions");
} catch (JSONException e) {
e.printStackTrace();
}
/** Invoking getPlaces with the array of json object
* where each json object represent a place
*/
return getPlaces(jPlaces);
}
private List<HashMap<String, String>> getPlaces(JSONArray jPlaces){
int placesCount = jPlaces.length();
List<HashMap<String, String>> placesList = new ArrayList<HashMap<String,String>>();
HashMap<String, String> place = null;
/** Taking each place, parses and adds to list object */
for(int i=0; i<placesCount;i++){
try {
/** Call getPlace with place JSON object to parse the place */
place = getPlace((JSONObject)jPlaces.get(i));
placesList.add(place);
} catch (JSONException e) {
e.printStackTrace();
}
}
return placesList;
}
/** Parsing the Place JSON object */
private HashMap<String, String> getPlace(JSONObject jPlace){
HashMap<String, String> place = new HashMap<String, String>();
String id="";
String reference="";
String description="";
try {
description = jPlace.getString("description");
id = jPlace.getString("id");
reference = jPlace.getString("reference");
place.put("description", description);
place.put("_id",id);
place.put("reference",reference);
} catch (JSONException e) {
e.printStackTrace();
}
return place;
}
}
custom autocomplete.java
package in.wptrafficanalyzer.locationplacesautocomplete;
import java.util.HashMap;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.AutoCompleteTextView;
/** Customizing AutoCompleteTextView to return Place Description
* corresponding to the selected item
*/
public class CustomAutoCompleteTextView extends AutoCompleteTextView {
public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/** Returns the place description corresponding to the selected item */
#Override
protected CharSequence convertSelectionToString(Object selectedItem) {
/** Each item in the autocompetetextview suggestion list is a hashmap object */
HashMap<String, String> hm = (HashMap<String, String>) selectedItem;
return hm.get("description");
}
}
mainactivity.java
package in.wptrafficanalyzer.locationplacesautocomplete;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.widget.AutoCompleteTextView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
AutoCompleteTextView atvPlaces;
PlacesTask placesTask;
ParserTask parserTask;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
atvPlaces = (AutoCompleteTextView) findViewById(R.id.atv_places);
atvPlaces.setThreshold(1);
atvPlaces.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
placesTask = new PlacesTask();
placesTask.execute(s.toString());
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
/** A method to download json data from url */
private String downloadUrl(String strUrl) throws IOException{
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try{
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while( ( line = br.readLine()) != null){
sb.append(line);
}
data = sb.toString();
br.close();
}catch(Exception e){
Log.d("Exception while downloading url", e.toString());
}finally{
iStream.close();
urlConnection.disconnect();
}
return data;
}
// Fetches all places from GooglePlaces AutoComplete Web Service
private class PlacesTask extends AsyncTask<String, Void, String>{
#Override
protected String doInBackground(String... place) {
// For storing data from web service
String data = "";
// Obtain browser key from https://code.google.com/apis/console
String key = "key=AIzaSyC6EKDiK_7XGJMd9YZ4ve5dvQ421Xwxx6s";
String input="";
try {
input = "input=" + URLEncoder.encode(place[0], "utf-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
// place type to be searched
String types = "types=geocode";
// Sensor enabled
String sensor = "sensor=false";
// Building the parameters to the web service
String parameters = input+"&"+types+"&"+sensor+"&"+key;
// Output format
String output = "json";
// Building the url to the web service
String url = "https://maps.googleapis.com/maps/api/place/autocomplete/"+output+"?"+parameters;
try{
// Fetching the data from web service in background
data = downloadUrl(url);
}catch(Exception e){
Log.d("Background Task",e.toString());
}
return data;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// Creating ParserTask
parserTask = new ParserTask();
// Starting Parsing the JSON string returned by Web Service
parserTask.execute(result);
}
}
/** A class to parse the Google Places in JSON format */
private class ParserTask extends AsyncTask<String, Integer, List<HashMap<String,String>>>{
JSONObject jObject;
#Override
protected List<HashMap<String, String>> doInBackground(String... jsonData) {
List<HashMap<String, String>> places = null;
PlaceJSONParser placeJsonParser = new PlaceJSONParser();
try{
jObject = new JSONObject(jsonData[0]);
// Getting the parsed data as a List construct
places = placeJsonParser.parse(jObject);
}catch(Exception e){
Log.d("Exception",e.toString());
}
return places;
}
#Override
protected void onPostExecute(List<HashMap<String, String>> result) {
String[] from = new String[] { "description"};
int[] to = new int[] { android.R.id.text1 };
// Creating a SimpleAdapter for the AutoCompleteTextView
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), result, android.R.layout.simple_list_item_1, from, to);
// Setting the adapter
atvPlaces.setAdapter(adapter);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
i have generated API key and inserted in mainactivity class, am new for android is there any other sources to learn about this?
Related
I'm developing an app and now I have to pass a parameter to a RESTful Service's URL. I'm using AsyncTask, and I need to pass a text from a list view as a parameter to the URL, for example: the URL is http://ip:7001/product?product_name=PARAM I need to get the text from the selected item from my list view, and pass as a parameter in PARAM, using AsyncTask. I've already got the text from the item in the listView, now I just need to pass it as a parameter.
This is my AsycTask class:
package com.tumta.henrique.teste;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import com.tumta.henrique.teste.ProdutoFragment;
/**
* Created by Henrique on 18/05/2015.
*/
public class FiltraProduto extends AsyncTask<String, Void, List<String>> {
private ConsultaConcluidaFiltroProdutoListener listener;
public static String URL_STRING = "http://192.168.0.20:7001/com.henrique.rest/api/v1/status/pro_filtro?pro_nome=";
public FiltraProduto(ConsultaConcluidaFiltroProdutoListener listener) {
this.listener = listener;
}
private List<String> InterpretaResultado(String resultado) throws JSONException {
JSONObject object = new JSONObject(resultado);
JSONArray jsonArray = object.getJSONArray("produto");
//JSONObject jsonProduto = jsonArray.getJSONObject(0);
// String id = jsonProduto.getString("pro_id");
//proId = id;
List<Object> listaNomes = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonProdutoInfo = jsonArray.getJSONObject(i);
String proNome= jsonProdutoInfo.getString("pro_nome");
double proPreco = jsonProdutoInfo.getDouble("pro_preco");
double proSdAtual = jsonProdutoInfo.getDouble("pro_sdAtual");
listaNomes.add(i, proNome);
listaNomes.add(i, proPreco);
listaNomes.add(i, proSdAtual);
}
List<String> strings = new ArrayList<String>();
for (Object o : listaNomes) {
strings.add(o != null ? o.toString() : null);
}
return strings;
}
private String ConsultaServidor() throws IOException {
InputStream is = null;
try {
URL url = new URL(URL_STRING);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(10000);
conn.setReadTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
conn.getResponseCode();
is = conn.getInputStream();
Reader reader = null;
reader = new InputStreamReader(is);
char[] buffer = new char[2048];
reader.read(buffer);
return new String(buffer);
} finally {
if (is != null) {
is.close();
}
}
}
#Override
protected List<String> doInBackground(String... params) {
try {
String resultado = ConsultaServidor();
return InterpretaResultado(resultado);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(List<String> result) {
listener.onConsultaConcluida(result);
super.onPostExecute(result);
}
public interface ConsultaConcluidaFiltroProdutoListener {
void onConsultaConcluida(List<String> result);
}
}
In the URL_STRING I need to pass the param at pro_nome=?
Here I get the item text. This is in my Fragment that has the List View:
public String retornaParam(String param){
return param;
}
#Override
public void onConsultaConcluida(List<String> result) {
final ListView listaProdutos = (ListView) getView().findViewById(R.id.listaprodutos);
ArrayAdapter arrayAdapter = new ArrayAdapter<>(getView().getContext(),android.R.layout.simple_list_item_1, result);
listaProdutos.setAdapter(arrayAdapter);
listaProdutos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
long id) {
String nomeProduto = listaProdutos.getItemAtPosition(position).toString();
retornaParam(nomeProduto);
Intent intent = new Intent(getActivity(), DetalhesProdutoActivity.class);
//intent.putExtra("pro_nome", listaProdutos.getItemAtPosition(position).toString());
startActivity(intent);
}
});
}
I get the text and store it in param from the retornaParam method.
Does somebody know how to do it?
If you need more information, just let me know.
You pass in params to an AsyncTask using:
YourAsyncTask.execute(yourview.getText(), "and", "more", "params");
You can then access them in
#Override
protected String doInBackground(String... params) {
URL_STRING += params[0];
...
Just add the following code before sending executing your httpClient:
URL_STRING + = textInsideYourTextView;
It should work, just avoid to manipulate your ui elements outside your UI thread.
package com.example.googlemapstestproject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.GoogleMap.OnMyLocationButtonClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends ActionBarActivity implements OnMapLongClickListener, OnMyLocationButtonClickListener,
android.view.View.OnClickListener {
private GoogleMap mMap;
Button userLocation;`enter code here`
GPSTracker gps;
PlacesTask placesTask;
ParserTask parserTask;
AutoCompleteTextView autoCompView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
autoCompView = (AutoCompleteTextView) findViewById(R.id.atv_places);
autoCompView.setThreshold(1);
autoCompView.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
placesTask = new PlacesTask();
placesTask.execute(s.toString());
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
mMap.setOnMapLongClickListener(this);
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
}
private String downloadUrl(String strUrl) throws IOException {
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try {
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
data = sb.toString();
br.close();
} catch (Exception e) {
Log.d("Exception while downloading url", e.toString());
}
finally {
iStream.close();
urlConnection.disconnect();
}
return data;
}
// Fetches all places from GooglePlaces AutoComplete Web Service
private class PlacesTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... place) {
// For storing data from web service
String data = "";
// Obtain browser key from https://code.google.com/apis/console
String key = "key=AIzaSyDTg7d-JNRLRxe75QDEEeAGr1xnSHGX9V4";
String input = "";
try {
input = "input=" + URLEncoder.encode(place[0], "utf-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
// place type to be searched
String types = "types=(cities)";
// Sensor enabled
String sensor = "sensor=false";
// Building the parameters to the web service
String parameters = input + "&" + types + "&" + sensor + "&" + key;
// Output format
String output = "json";
// Building the url to the web service
String url = "https://maps.googleapis.com/maps/api/place/autocomplete/" + output + "?" + parameters;
try {
// Fetching the data from we service
data = downloadUrl(url);
} catch (Exception e) {
Log.d("Background Task", e.toString());
}
return data;
}
// Executed after the complete execution of doInBackground() method
#Override
protected void onPostExecute(String result) {
// Instantiating ParserTask which parses the json data from
// Geocoding webservice
// in a non-ui thread
ParserTask parserTask = new ParserTask();
// Start parsing the places in JSON format
// Invokes the "doInBackground()" method of the class ParseTask
parserTask.execute(result);
}
}
// A class to parse the Google Places in JSON format
private class ParserTask extends AsyncTask<String, Integer, List<HashMap<String, String>>> {
JSONObject jObject;
#Override
protected List<HashMap<String, String>> doInBackground(String... jsonData) {
List<HashMap<String, String>> places = null;
PlaceJSONParser placeJsonParser = new PlaceJSONParser();
try {
jObject = new JSONObject(jsonData[0]);
// Getting the parsed data as a List
places = placeJsonParser.parse(jObject);
} catch (Exception e) {
Log.d("Exception", e.toString());
}
return places;
}
#Override
protected void onPostExecute(List<HashMap<String, String>> result) {
String[] from = new String[] { "description" };
int[] to = new int[] { R.layout.listview_layout };
// Creating a SimpleAdapter for the AutoCompleteTextView
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), result, android.R.layout.simple_list_item_1, from, to);
// Setting the adapter
autoCompView.setAdapter(adapter);
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Updated code update, everything looks fine it is just that there is nothing appearing still when I type in a place.
I have made the google maps half and half with a listview as well so if anyone has any good solutions on how to get them to work together that would be great.
If you would like to use a library that provides a GooglePlaceAutoComplete widget, check out Sprockets (I'm the developer). After setting it up with your API key, you could add a working Places API autocomplete to your layout with something like:
<net.sf.sprockets.widget.GooglePlaceAutoComplete
xmlns:sprockets="http://schemas.android.com/apk/res-auto"
android:id="#+id/place"
android:layout_width="match_parent"
android:layout_height="wrap_content"
sprockets:types="(cities)"/>
Hi I'm having an issue trying to retrieve information from an async task.
What I'm trying to achieve is get the lng/lat from a postcode and then display them on a map. Getting the lng/lat works fine, its just I cant work out how to get that information from the onPostExecute back to the onCreate in an array.
I can perform a loop to add the map markers there.
Anyone know how I go about this?
Updated code
This is the working code that also updates the zoom to fit all entries in. Might help some people out
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import com.example.coreoffice.library.GeocodeJSONParser;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.LatLngBounds.Builder;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity
{
Button mBtnFind;
static GoogleMap mMap;
EditText etPlace;
private static final String TAG = "MainActivity";
static ArrayList<Double> lat = new ArrayList<Double>();
static ArrayList<Double> lng = new ArrayList<Double>();
static Builder bounds = new LatLngBounds.Builder();
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_maps);
Log.v(TAG + "onCreate", "onCreate call");
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap = mapFragment.getMap();
String[] location =
{ "WA32ED", "M30oft", "W21AA" }; // address
String url;
for (int i = 0; i < location.length; i++)
{
DownloadTask downloadTask = new DownloadTask();
url = "";
url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + location[i] + "&sensor=false";
Log.v("URL", url);
downloadTask.execute(url);
}
}
public static void processMap()
{
for (int i = 0; i < lat.size(); i++)
{
MarkerOptions markerOptions = new MarkerOptions();
LatLng latLng = new LatLng(lat.get(i), lng.get(i));
markerOptions.position(latLng);
markerOptions.title("title");
mMap.addMarker(markerOptions);
bounds.include(new LatLng(lat.get(i), lng.get(i)));
}
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds.build(), 100));
}
private String downloadUrl(String strUrl) throws IOException
{
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try
{
URL url = new URL(strUrl);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null)
{
sb.append(line);
}
data = sb.toString();
br.close();
}
catch (Exception e)
{
Log.d("Exception while downloading url", e.toString());
}
finally
{
iStream.close();
urlConnection.disconnect();
}
return data;
}
private class DownloadTask extends AsyncTask<String, Integer, String>
{
String data = null;
#Override
protected String doInBackground(String... url)
{
try
{
data = downloadUrl(url[0]);
}
catch (Exception e)
{
Log.d("Background Task", e.toString());
}
return data;
}
#Override
protected void onPostExecute(String result)
{
ParserTask parserTask = new ParserTask();
parserTask.execute(result);
}
}
class ParserTask extends AsyncTask<String, Integer, List<HashMap<String, String>>>
{
JSONObject jObject;
#Override
protected List<HashMap<String, String>> doInBackground(String... jsonData)
{
List<HashMap<String, String>> places = null;
GeocodeJSONParser parser = new GeocodeJSONParser();
try
{
jObject = new JSONObject(jsonData[0]);
places = parser.parse(jObject);
}
catch (Exception e)
{
Log.d("Exception", e.toString());
}
return places;
}
#Override
protected void onPostExecute(List<HashMap<String, String>> list)
{
for (int i = 0; i < list.size(); i++)
{
HashMap<String, String> hmPlace = list.get(i);
double lat = Double.parseDouble(hmPlace.get("lat"));
double lng = Double.parseDouble(hmPlace.get("lng"));
MapsActivity.this.lat.add(lat);
MapsActivity.this.lng.add(lng);
MapsActivity.processMap();
Log.v("lat", Double.toString(lat));
Log.v("lng", Double.toString(lng));
}
}
}
}
As your AsyncTasks are, by definition, asynchronous, you cannot guarantee that they will be done before onCreate() finishes. You merely need to call a method in onPostExecute() of ParserTask to handle the "loop" you describe after the tasks have completed.
Your ArrayLists lat and lng are accessible to the inner classes that are your AsyncTasks, so you just need to add a few lines to onPostExecute():
protected void onPostExecute(List<HashMap<String, String>> list)
{
for (int i = 0; i < list.size(); i++)
{
HashMap<String, String> hmPlace = list.get(i);
double lat = Double.parseDouble(hmPlace.get("lat"));
double lng = Double.parseDouble(hmPlace.get("lng"));
MapsActivity.this.lat.add(lat);
MapsActivity.this.lng.add(lng);
Log.v("lat", Double.toString(lat));
Log.v("lng", Double.toString(lng));
}
processData();
}
Then create a method in MapsActivity:
private void processData()
{
// Do your loop here.
}
You can define an interface as callback for your ParserTask class, and call it in onPostExecute method.
First declare an interface:
public interface OnParseFinishedListener {
public void onFinished(/* required params */);
}
Then add these methods to your ParserTask:
public void setOnParseFinishedListener(OnParseFinishedListener listener){
this.listener = listener;
}
and in your onPostExecute add this line:
if(listener != null) {
listener.onFinished(/* pass params here! */);
}
Use a global variable to hold your HashMap, and then pass it from your Activity into your AsyncTask.
you Should do the donwloading and parsing in a single asynctask.
I would propose using loader instead of asynctask (as you will have a problem once your asynctask returns and your activity has been recreated (eg due to turning the screen or any other configuration change)
In onPostExecute you just call a method in your activity. There you can access your map object /fragment (which you store in an instance variable in onCreate).
Again use an asyncloader which behaves very similar to asynctask but will save you a lot of grief down the line even if it's slightly more complex in the beginning.
I have my json file like this:
{
"shops": [
{
"id": "11",
"name": "Next",
"description": "Opened in 2005, offers a selection of clothes, shoes and accessories.",
"url": "/shop/550/127",
"categories": [
"4",
"33",
"34",
"16"
],
"bg_image": "/uploads/static/shop/460px/2012/12/5385-127-sale.jpg"
},
.....
I want to fetch this shops according to the categories using "categories". For example if the men's jeans clicked from the list, then all shops associated with that categories displayed as a list. And this JSON file stored in sdcard. Currently I am able to fetch all the shops if a list item clicked. But I couldn't filter according to the categories.
Jean.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import org.json.JSONObject;
import com.kabelash.sg.util.ExternalStorage;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class Jeans extends Activity {
private final String JSON_file = "api_output_example.json";
File jsonFile;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.baby1_1);
/** Getting Cache Directory */
File cDir = ExternalStorage.getSDCacheDir( this, "json_files" );
/** Getting a reference to temporary file, if created earlier */
jsonFile = new File(cDir.getPath() + "/" + JSON_file) ;
String strLine="";
StringBuilder strJson = new StringBuilder();
/** Reading contents of the temporary file, if already exists */
try {
FileReader fReader = new FileReader(jsonFile);
BufferedReader bReader = new BufferedReader(fReader);
/** Reading the contents of the file , line by line */
while( (strLine=bReader.readLine()) != null ){
strJson.append(strLine);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
//System.out.println(strJson);
/** Start parsing JSON data */
new ListViewLoaderTask().execute(strJson.toString());
}
private class ListViewLoaderTask extends AsyncTask<String, Void, SimpleAdapter>{
JSONObject jObject;
/** Doing the parsing of JSON data in a non-ui thread */
#Override
protected SimpleAdapter doInBackground(String... strJson) {
try{
jObject = new JSONObject(strJson[0]);
CountryJSONParser countryJsonParser = new CountryJSONParser();
countryJsonParser.parse(jObject);
}catch(Exception e){
Log.d("JSON Exception1",e.toString());
}
CountryJSONParser countryJsonParser = new CountryJSONParser();
List<HashMap<String, String>> shops = null;
try{
/** Getting the parsed data as a List construct */
shops = countryJsonParser.parse(jObject);
}catch(Exception e){
Log.d("Exception",e.toString());
}
/** Keys used in Hashmap */
String[] from = { "shop","image","description"};
/** Ids of views in listview_layout */
int[] to = { R.id.tv_country,R.id.iv_flag,R.id.tv_country_details};
/** Instantiating an adapter to store each items
* R.layout.listview_layout defines the layout of each item
*/
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), shops, R.layout.lv_layout, from, to);
return adapter;
}
/** Invoked by the Android system on "doInBackground" is executed completely */
/** This will be executed in ui thread */
#Override
protected void onPostExecute(SimpleAdapter adapter) {
/** Getting a reference to listview of main.xml layout file */
ListView listView = ( ListView ) findViewById(R.id.lv_countries);
/** Setting the adapter containing the country list to listview */
listView.setAdapter(adapter);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
countryJSONParser.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class CountryJSONParser {
/** Receives a JSONObject and returns a list */
public List<HashMap<String,String>> parse(JSONObject jObject){
JSONArray jShops = null;
try {
/** Retrieves all the elements in the 'countries' array */
jShops = jObject.getJSONArray("shops");
} catch (JSONException e) {
e.printStackTrace();
}
/** Invoking getShops with the array of json object
* where each json object represent a country
*/
return getShops(jShops);
}
private List<HashMap<String, String>> getShops(JSONArray jShops){
int shopCount = jShops.length();
List<HashMap<String, String>> shopList = new ArrayList<HashMap<String,String>>();
HashMap<String, String> shop = null;
/** Taking each shop, parses and adds to list object */
for(int i=0; i<shopCount;i++){
try {
/** Call getShop with shop JSON object to parse the shop */
shop = getShop((JSONObject)jShops.get(i));
shopList.add(shop);
} catch (JSONException e) {
e.printStackTrace();
}
}
return shopList;
}
/** Parsing the shop JSON object */
private HashMap<String, String> getShop(JSONObject jShop){
HashMap<String, String> shop = new HashMap<String, String>();
String shopName = "";
String image="";
String description = "";
String categories = "";
//String capital = "";
try {
shopName = jShop.getString("name");
image = jShop.getString("bg_image_small");
description = jShop.getString("description");
categories = jShop.getString("categories");
String details = "Description : " + description;
//if (categories.equals("11")){
shop.put("shop", shopName);
shop.put("image", image);
shop.put("description", details);
shop.put("categories", categories);
//}
} catch (JSONException e) {
e.printStackTrace();
}
return shop;
}
}
The above given code work fine but I want to know how to filter it. and I want to display images on listview using "bg_image" link (the images are stored on sdcard). Can someone please edit this code according to my requirement? (Unfortunately I couldn't find anything useful on Google). Please help!
Edited: Now I tried to do something like this but couldn't get it right.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class ShopJSONParser {
public class Shop {
public String name;
public String category;
public String description;
public String image;
public ArrayList<String> category_list;
}
/** Receives a JSONObject and returns a list */
public List<HashMap<String,String>> parse(JSONObject jObject){
JSONArray jShops = null;
try {
// Retrieves all the elements in the 'shops' array
jShops = jObject.getJSONArray("shops");
} catch (JSONException e) {
e.printStackTrace();
}
// Invoking getShops with the array of json object
// where each json object represent a shop
return getShops(jShops);
}
ArrayList<Shop> shops_array = new ArrayList<Shop>();
private List<HashMap<String, String>> getShops(JSONArray jShops){
int shopCount = jShops.length();
List<HashMap<String, String>> shopList = new ArrayList<HashMap<String,String>>();
HashMap<String, String> shop = null;
/** Taking each shop, parses and adds to list object */
for (int i = 0; i < jShops.length(); i++) {
JSONObject Shop_json_obj = jShops.getJSONObject(i);
Shop shop1 = new Shop();
String name = Shop_json_obj.getString("name");
String description = Shop_json_obj.getString("description");
shop1.name = name;
shop1.description = description;
shop1.category_list = new ArrayList<String>();
JSONArray categories_json_array = null;
categories_json_array = shop1.getJSONArray("categories");
for (int j = 0; j < categories_json_array.length(); j++) {
String cat = categories_json_array.getString(j);
shop1.category_list.add(cat);
}
shops_array.add(shop1);
}
return shopList;
}
/** Parsing the shop JSON object */
private HashMap<String, String> getShop(JSONObject jShop){
HashMap<String, String> shop = new HashMap<String, String>();
String shopName = "";
String image="";
String description = "";
String categories = "";
try {
shopName = jShop.getString("name");
image = jShop.getString("bg_image_small");
description = jShop.getString("description");
categories = jShop.getJSONArray("categories").toString();
System.out.println(categories);
String details = "Description : " + description;
//if (categories.equals("13")){
shop.put("shop", shopName);
shop.put("image", image);
//shop.put("description", details);
//shop.put("categories", categories);
//}
} catch (JSONException e) {
e.printStackTrace();
}
return shop;
}
}
Further help required. Thanks.
Firstly, i think you are parsing it wrong. 'categories' is JsonArray while you are parsing it as a string. You should be doing it as an array. so, its basically an array inside an array. Once you are done with it, you could easily tag an individual item as the appropriate category based on the category value.
Edit:
A better way to do that would be to create a Shop object with desired attributes and then Populate a List of Shop objects in your JSON parsing function. e.g
public class Shop{
public String name;
public String description;
public String image;
public ArrayList<String> category_list;
public Shop(){
category_list = new ArrayList<String>();
}
}
and here is your modified CountryJSONParser :
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class CountryJSONParser {
/** Receives a JSONObject and returns a list */
public ArrayList<Shop> parse(JSONObject jObject){
JSONArray jShops = null;
try {
/** Retrieves all the elements in the 'countries' array */
jShops = jObject.getJSONArray("shops");
} catch (JSONException e) {
e.printStackTrace();
}
/** Invoking getShops with the array of json object
* where each json object represent a country
*/
return getShops(jShops);
}
private ArrayList<Shop> getShops(JSONArray jShops){
int shopCount = jShops.length();
ArrayList<Shop> shops_array = new ArrayList<Shop>();
Shop shop = new Shop();
/** Taking each shop, parses and adds to list object */
for(int i=0; i<shopCount;i++){
try {
/** Call getShop with shop JSON object to parse the shop */
shop = getShop((JSONObject)jShops.get(i));
//shopList.add(shop);
shops_array.add(shop);
} catch (JSONException e) {
e.printStackTrace();
}
}
return shops_array;
}
/** Parsing the shop JSON object */
private Shop getShop(JSONObject jShop){
Shop shop = new Shop();
try {
shop.name = jShop.getString("name");
shop.image = jShop.getString("bg_image_small");
String details = "Description : " + shop.description;
shop.description = details;
shop.category_list = new ArrayList<String>();
JSONArray categories_json_array = null;
categories_json_array = jShop.getJSONArray("categories");
for (int j = 0; j < categories_json_array.length(); j++) {
String cat = categories_json_array.getString(j);
shop.category_list.add(cat);
}
} catch (JSONException e) {
e.printStackTrace();
}
return shop;
}
}
I am new to android. I am implementing a project in which I want to get the data from the back end and display on my android screen.
The Json I am trying to call is:-
[{"admin":null,"card_no":"8789","created_at":"2013-04-09T12:55:54Z","deleted":0,"email":"dfds#fgfd.com","entered_by":null,"first_name":"Gajanan","id":8,"last_name":"Bhat","last_updated_by":null,"middle_name":"","mobile":87981,"updated_at":"2013-04-13T05:26:25Z","user_type_id":null},{"admin":{"created_at":"2013-04-10T09:02:00Z","deleted":0,"designation":"Sr software Engineer","email":"admin#qwe.com","first_name":"Chiron","id":1,"last_name":"Synergies","middle_name":"Sr software Engineer","office_phone":"98789765","super_admin":false,"updated_at":"2013-04-10T12:03:04Z","username":"Admin"},"card_no":"66","created_at":"2013-04-08T09:47:15Z","deleted":0,"email":"rajaarun1991","entered_by":1,"first_name":"Arun","id":1,"last_name":"Raja\n","last_updated_by":1,"middle_name":"Nagaraj","mobile":941,"updated_at":"2013-04-08T09:47:15Z","user_type_id":1}]
My JsonParser.java is as follows:-
package com.example.library;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONArray jarray = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONArray getJSONFromUrl(String url) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e("==>", "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// try parse the string to a JSON object
try {
jarray = new JSONArray( builder.toString());
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jarray;
}
}
/* public void writeJSON() {
JSONObject object = new JSONObject();
try {
object.put("name", "");
object.put("score", new Integer(200));
object.put("current", new Double(152.32));
object.put("nickname", "Programmer");
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println(object);
}
*/
And my activity.java is as follows:-
package com.example.library;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class SecondActivity extends Activity
{
private Context context;
private static String url = "http://192.168.0.100:3000/users.json";
private static final String TAG_ID = "id";
private static final String TAG_FIRST_NAME = "first_name";
private static final String TAG_MIDDLE_NAME = "middle_name";
private static final String TAG_LAST_NAME = "last_name";
// private static final String TAG_POINTS = "experiencePoints";
ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
ListView lv ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ProgressTask(SecondActivity.this).execute();
}
private class ProgressTask extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog;
public ProgressTask(SecondActivity secondActivity) {
Log.i("1", "Called");
context = secondActivity;
dialog = new ProgressDialog(context);
}
/** progress dialog to show user that the backup is processing. */
/** application context. */
private Context context;
protected void onPreExecute() {
this.dialog.setMessage("Progress start");
this.dialog.show();
}
#Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}
ListAdapter adapter = new SimpleAdapter(context, jsonlist,
R.layout.list_item, new String[] { TAG_ID, TAG_FIRST_NAME,
TAG_MIDDLE_NAME, TAG_LAST_NAME }, new int[] {
R.id.id, R.id.first_name, R.id.middle_name,
R.id.last_name });
setListAdapter(adapter);
// selecting single ListView item
lv = getListView();
}
private void setListAdapter(ListAdapter adapter) {
// TODO Auto-generated method stub
}
protected Boolean doInBackground(final String... args) {
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONArray json = jParser.getJSONFromUrl(url);
for (int i = 0; i < json.length(); i++) {
try {
JSONObject c = json.getJSONObject(i);
String id = c.getString(TAG_ID);
String first_name = c.getString(TAG_FIRST_NAME);
String middle_name = c.getString(TAG_MIDDLE_NAME);
String last_name = c.getString(TAG_LAST_NAME);
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_FIRST_NAME, first_name);
map.put(TAG_MIDDLE_NAME, middle_name);
map.put(TAG_LAST_NAME, last_name);
jsonlist.add(map);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
}
public ListView getListView() {
// TODO Auto-generated method stub
return null;
}
}
I am not able to show anything on the emulators for users,whereas I am able to fetch the data from the server
Actually in the project I want to display all the users present in the library.I am accessing the server which is on Ubuntu.
The following message is displayed on the console(Terminal):-
Started GET "/users.json" for 192.168.0.104 at 2013-05-05 22:05:01 -0700
Processing by UsersController#index as JSON
User Load (0.4ms) SELECT "users".* FROM "users" WHERE (users.deleted = 0) ORDER BY users.id DESC
Admin Load (0.3ms) SELECT "admins".* FROM "admins" WHERE "admins"."id" = 1 AND (admins.deleted = 0) ORDER BY admins.id DESC LIMIT 1
Completed 200 OK in 4ms (Views: 2.1ms | ActiveRecord: 0.6ms)
[2013-05-05 22:05:01] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
However i am not able to display the data on the android screen/emulator.
Please help me with this.
I missed your setAdapterList()method.
Any error for your json pasers? and can you put your activity which is inherited from SecondActivity?
//modify getListView
public ListView getListView() {
// TODO Auto-generated method stub
listView = (ListView)findViewById(r.your.listview);
return listView;
}
//modify setListAdapter
private void setListAdapter(ListAdapter adapter) {
// TODO Auto-generated method stub
lv.setAdapter(adapter);
}