Clicking event of PolyGon in GoogleMap - android
I am trying to make a clickable PolyGon.Even I tried all the method that have already answered in this StackOverFlow. But nothing is worked.
My code is as follows,
JSONObject jsonObj = new JSONObject(result);
final JSONArray jsonArray = jsonObj.getJSONArray("zones");
int i;
System.out.println("PolyGon---> Response jsonArray " + jsonArray);
for (i = 0; i < jsonArray.length(); i++) {
JSONArray jsonArrayPoly = jsonArray.getJSONObject(i).getJSONArray("coordinates");
System.out.println("PolyGon---> Response coordinates " + jsonArrayPoly);
polygonOptions = new PolygonOptions();
polygonOptions.strokeColor(Color.BLACK);
polygonOptions.strokeWidth(2);
polygonOptions.fillColor(getResources().getColor(R.color.zone));
for (int j = 0; j < jsonArrayPoly.length(); j++) {
JSONObject jsonCoordinate = jsonArrayPoly.getJSONObject(j);
System.out.println("PolyGon---> Response lat " + jsonCoordinate.getDouble("lat") + " ----> Long");
polygonOptions.add(new LatLng(jsonCoordinate.getDouble("lat"), jsonCoordinate.getDouble("lng")));
}
googleMap.addPolygon(polygonOptions);
}
polygonOptions.clickable(true);
googleMap.setOnPolygonClickListener(new GoogleMap.OnPolygonClickListener() {
public void onPolygonClick(Polygon polygon) {
Toast.makeText(getApplicationContext(), "Problem reading list of markers.", Toast.LENGTH_LONG).show();
}
});
Your answer is to be more appreciated.
Set your PolygonOptions.clickable before adding your polygon to the map:
polygonOptions.clickable(true);
googleMap.addPolygon(polygonOptions);
Related
"Not a primitive array" error when I try to pull a child from JSON
I'm trying to pull an array from this JSON: { \"name\": { \"mainName\":\"Ham and cheese sandwich\", \"alsoKnownAs\":[\"Steamed bao\",\"Pork belly bun\"] }, But I get an error, "Not a primitive array", when I write this code: JSONObject sandwich = new JSONObject(json); JSONObject name = sandwich.getJSONObject("name"); String mainName = name.getString("mainName"); JSONArray alsoKnownAsArray = new JSONArray(name.getJSONArray("alsoKnownAs")); for (int i = 0; i < alsoKnownAsArray.length(); i++) { String alsoKnown = alsoKnownAsArray.getString(i); Log.v("TAG", alsoKnown); } I know there's something wrong with the with the JSONArray code, but don't know what it is. I searched and didn't find out how to get an array when it is a child.
The answer: JSONArray alsoKnownAsArray = name.getJSONArray("alsoKnownAs"); for (int i = 0; i < alsoKnownAsArray.length(); i++) { String alsoKnown = alsoKnownAsArray.getString(i); Log.v("TAG", alsoKnown); }
Nested JSONArray Record Parsing Issue Android
I am making a App which receives a JSON Data From Server, User will select a value from Spinner like 0,1,2... and so on, On the Basis of number selection JSON will return a data from user defined index like 0,1.. i don't know how to parse a inner JSON Data in Android json = new JSONObject(JSONParser.Result); JSONArray jArray = json.getJSONArray("data"); StopElement _stop = new StopElement(); Log.d("JSON Algo Result", json.toString()); if (jArray!=null) { for (int i = 0; i < jArray.length(); i++) { jsonarray = jArray.getJSONArray(i); if (jsonarray != null) { for (int j = 0; j < jsonarray.length(); i++){ if(i==0) { jsonarray = jArray.getJSONArray(j); _stop.setName(jsonarray.getString(0)); StopElement.Stop_name_list.add(_stop.getName()); } } } else { break; } } } else { return null; } **JSON Data** [[["Sadiqabad Chowk","33.634525","73.074326","suzk1"],["Chungi No 8","33.627262","73.093567","suzk1"],["Jahaz Ground","33.628395","73.101936","suzk1"],["Khana Bridge","33.629967","73.112823","suzk1"],["Kuri Road","33.643162","73.102928","21"],["Faizabad","33.663212","73.084801","21"]],[["Sadiqabad Chowk","33.634525","73.074326","suzk1"],["Petrol Pump","33.634109","73.076363","suzk1"],["Chandni Chowk","33.631584","73.072563","suzk1"],["Rahmanabad","33.639065","73.075714","3"],["Passport Office","33.642410","73.076981","3"],["Shamsabad","33.650101","73.079994","3"],["Faizabad","33.663212","73.084801","3"]],[["Sadiqabad Chowk","33.634525","73.074326","suzk1"],["Chungi No 8","33.627262","73.093567","suzk1"],["Jahaz Ground","33.628395","73.101936","suzk1"],["Khana Bridge","33.629967","73.112823","suzk1"],["Zia Masjid","33.637196","73.107407","124-A"],["Kuri Road","33.643162","73.102928","124-A"],["Dhok Kala Khan","33.653118","73.095444","124-A"],["Faizabad","33.663212","73.084801","124-A"]],[["Sadiqabad Chowk","33.634525","73.074326","suzk1"],["Chungi No 8","33.627262","73.093567","suzk1"],["Jahaz Ground","33.628395","73.101936","suzk1"],["Khana Bridge","33.629967","73.112823","suzk1"],["Zia Masjid","33.637196","73.107407","136"],["Kuri Road","33.643162","73.102928","136"],["Iqbal Town","33.644279","73.100113","136"],["Dhok Kala Khan","33.653118","73.095444","136"],["Faizabad","33.663212","73.084801","136"]],[["Sadiqabad Chowk","33.634525","73.074326","suzk13"],["Dhok Ali Akbar","33.636997","73.092117","suzk13"],["Highway Stop","33.679722","73.075584","suzk13"],["Bhinder","33.556244","73.167946","suzk3"],["Lohi Bher","33.586273","73.145493","124"],["Wild Life Park","33.578770","73.132309","124"],["Airport Chowk","33.593803","73.139938","124"],["Gangal","33.612591","73.125801","124"],["Khana Bridge","33.629967","73.112823","124"],["Zia Masjid","33.637196","73.107407","124"],["Kuri Road","33.643162","73.102928","124"],["Dhok Kala Khan","33.653118","73.095444","124"],["Faizabad","33.663212","73.084801","124"]],[["Sadiqabad Chowk","33.634525","73.074326","suzk13"],["Petrol Pump","33.634109","73.076363","suzk13"],["Chandni Chowk","33.631584","73.072563","suzk13"],["Central Hospital","33.628784","73.070641","suzk13"],["Naz Cinema","33.623936","73.067802","suzk13"],["Waris Khan","33.620728","73.066078","suzk13"],["Comittee Chowk","33.612946","73.065193","suzk13"],["Liaquat Bagh","33.606808","73.064835","suzk13"],["Marir Hassan","33.596905","73.064445","suzk7"],["Punjab House","33.592701","73.065453","suzk7"],["Jhanda","33.588970","73.076195","suzk7"],["Raheemabad","33.599159","73.080048","21"],["Airport","33.603565","73.097137","21"],["Tajabad","33.601593","73.126213","21"],["Koral Chowk","33.605282","73.131279","21"],["Khana Bridge","33.629967","73.112823","21"],["Kuri Road","33.643162","73.102928","21"],["Faizabad","33.663212","73.084801","21"]],[["Sadiqabad Chowk","33.634525","73.074326","suzk13"],["Petrol Pump","33.634109","73.076363","suzk13"],["Chandni Chowk","33.631584","73.072563","suzk13"],["Central Hospital","33.628784","73.070641","suzk13"],["Naz Cinema","33.623936","73.067802","suzk13"],["Waris Khan","33.620728","73.066078","suzk13"],["Comittee Chowk","33.612946","73.065193","suzk13"],["Liaquat Bagh","33.606808","73.064835","suzk13"],["Medical College","33.602753","73.067200","29"],["Sir Syed Boys School","33.609600","73.078766","29"],["Fauji Tower","33.606770","73.084106","29"],["Chaklala Station","33.601013","73.095924","29"],["Raheemabad","33.599159","73.080048","29"],["Airport","33.603565","73.097137","21"],["Tajabad","33.601593","73.126213","21"],["Koral Chowk","33.605282","73.131279","21"],["Khana Bridge","33.629967","73.112823","21"],["Kuri Road","33.643162","73.102928","21"],["Faizabad","33.663212","73.084801","21"]],[["Sadiqabad Chowk","33.634525","73.074326","suzk13"],["Petrol Pump","33.634109","73.076363","suzk13"],["Chandni Chowk","33.631584","73.072563","suzk13"],["Central Hospital","33.628784","73.070641","suzk13"],["Naz Cinema","33.623936","73.067802","suzk13"]]]
this will work definitely for you. String data= "[[['Jahaz Ground','33.628395','73.101936','suzk1'],['Khana Bridge','33.628395','73.101936','suzk1']]]"; try { JSONArray json = new JSONArray(data); JSONArray aray = (JSONArray)json.get(0); for(int i =0; i< aray.length();i++){ JSONArray object = (JSONArray)aray.get(i); String title = (String)object .get(0); String lat = (String)object.get(1); String lng = (String)object.get(2); String com = (String)object.get(3); } } catch (JSONException e) { e.printStackTrace(); }
try this way, U can get result as per your request private String jsonString="[[['Sadiqabad Chowk','33.634525','73.074326','suzk1'],['Chungi No 8','33.627262','73.093567','suzk1'],['Jahaz Ground','33.628395','73.101936','suzk1'],['Khana Bridge','33.629967','73.112823','suzk1'],['Kuri Road','33.643162','73.102928','21'],['Faizabad','33.663212','73.084801','21']],[['Sadiqabad Chowk','33.634525','73.074326','suzk1'],['Petrol Pump','33.634109','73.076363','suzk1'],['Chandni Chowk','33.631584','73.072563','suzk1'],['Rahmanabad','33.639065','73.075714','3'],['Passport Office','33.642410','73.076981','3'],['Shamsabad','33.650101','73.079994','3'],['Faizabad','33.663212','73.084801','3']],[['Sadiqabad Chowk','33.634525','73.074326','suzk1'],['Chungi No 8','33.627262','73.093567','suzk1'],['Jahaz Ground','33.628395','73.101936','suzk1'],['Khana Bridge','33.629967','73.112823','suzk1'],['Zia Masjid','33.637196','73.107407','124-A'],['Kuri Road','33.643162','73.102928','124-A'],['Dhok Kala Khan','33.653118','73.095444','124-A'],['Faizabad','33.663212','73.084801','124-A']],[['Sadiqabad Chowk','33.634525','73.074326','suzk1'],['Chungi No 8','33.627262','73.093567','suzk1'],['Jahaz Ground','33.628395','73.101936','suzk1'],['Khana Bridge','33.629967','73.112823','suzk1'],['Zia Masjid','33.637196','73.107407','136'],['Kuri Road','33.643162','73.102928','136'],['Iqbal Town','33.644279','73.100113','136'],['Dhok Kala Khan','33.653118','73.095444','136'],['Faizabad','33.663212','73.084801','136']],[['Sadiqabad Chowk','33.634525','73.074326','suzk13'],['Dhok Ali Akbar','33.636997','73.092117','suzk13'],['Highway Stop','33.679722','73.075584','suzk13'],['Bhinder','33.556244','73.167946','suzk3'],['Lohi Bher','33.586273','73.145493','124'],['Wild Life Park','33.578770','73.132309','124'],['Airport Chowk','33.593803','73.139938','124'],['Gangal','33.612591','73.125801','124'],['Khana Bridge','33.629967','73.112823','124'],['Zia Masjid','33.637196','73.107407','124'],['Kuri Road','33.643162','73.102928','124'],['Dhok Kala Khan','33.653118','73.095444','124'],['Faizabad','33.663212','73.084801','124']],[['Sadiqabad Chowk','33.634525','73.074326','suzk13'],['Petrol Pump','33.634109','73.076363','suzk13'],['Chandni Chowk','33.631584','73.072563','suzk13'],['Central Hospital','33.628784','73.070641','suzk13'],['Naz Cinema','33.623936','73.067802','suzk13'],['Waris Khan','33.620728','73.066078','suzk13'],['Comittee Chowk','33.612946','73.065193','suzk13'],['Liaquat Bagh','33.606808','73.064835','suzk13'],['Marir Hassan','33.596905','73.064445','suzk7'],['Punjab House','33.592701','73.065453','suzk7'],['Jhanda','33.588970','73.076195','suzk7'],['Raheemabad','33.599159','73.080048','21'],['Airport','33.603565','73.097137','21'],['Tajabad','33.601593','73.126213','21'],['Koral Chowk','33.605282','73.131279','21'],['Khana Bridge','33.629967','73.112823','21'],['Kuri Road','33.643162','73.102928','21'],['Faizabad','33.663212','73.084801','21']],[['Sadiqabad Chowk','33.634525','73.074326','suzk13'],['Petrol Pump','33.634109','73.076363','suzk13'],['Chandni Chowk','33.631584','73.072563','suzk13'],['Central Hospital','33.628784','73.070641','suzk13'],['Naz Cinema','33.623936','73.067802','suzk13'],['Waris Khan','33.620728','73.066078','suzk13'],['Comittee Chowk','33.612946','73.065193','suzk13'],['Liaquat Bagh','33.606808','73.064835','suzk13'],['Medical College','33.602753','73.067200','29'],['Sir Syed Boys School','33.609600','73.078766','29'],['Fauji Tower','33.606770','73.084106','29'],['Chaklala Station','33.601013','73.095924','29'],['Raheemabad','33.599159','73.080048','29'],['Airport','33.603565','73.097137','21'],['Tajabad','33.601593','73.126213','21'],['Koral Chowk','33.605282','73.131279','21'],['Khana Bridge','33.629967','73.112823','21'],['Kuri Road','33.643162','73.102928','21'],['Faizabad','33.663212','73.084801','21']],[['Sadiqabad Chowk','33.634525','73.074326','suzk13'],['Petrol Pump','33.634109','73.076363','suzk13'],['Chandni Chowk','33.631584','73.072563','suzk13'],['Central Hospital','33.628784','73.070641','suzk13'],['Naz Cinema','33.623936','73.067802','suzk13']]]"; private void getJSONResult(String jsonString) { // TODO Auto-generated method stub try { JSONArray jsonArray = new JSONArray(jsonString); // i.e. the JsonMainArray JSONArray JsonInnerArray= (JSONArray)jsonArray.get(0); // i.e. the JsonInnerArray for(int i =0; i< JsonInnerArray.length();i++){ JSONArray jsonObject = (JSONArray)JsonInnerArray.get(i); //i.e. the jsonObject of JsonInnerArray String title = (String)jsonObject .get(0); String latitude = (String)jsonObject.get(1); String longitude = (String)jsonObject.get(2); String companyName = (String)jsonObject.get(3); } } catch (JSONException e) { e.printStackTrace(); } }
I Solved My Issue like this try { int count = 0; if (success == 1) { json = new JSONObject(JSONParser.Result); jArray = json.getJSONArray("data"); StopElement _stop = new StopElement(); for (int i = 0; i < jArray.length(); i++) { count = count + 1; // Log.d("Counter", String.valueOf(count)); get_path.add(String.valueOf(count)); jsonarray = jArray.getJSONArray(i); Log.d("path Array", jsonarray.toString()); if (getitemno>0) { getpath = jArray.getJSONArray(getitemno); for (int j = 0; j < getpath.length(); j++) { getresult = getpath.getJSONArray(j); _stop.setName(getresult.getString(0)); _map.setLat(getresult.getDouble(1)); _map.setLng(getresult.getDouble(2)); StopElement.Stop_name_list.add(_stop.getName()); MapCoordinatesElement.Lat.add(_map.getLat()); MapCoordinatesElement.Lng.add(_map.getLng()); //Log.d("Stop names", _stop.getName()); // Log.d("Index Array", getpath.toString()); } } else { getpath = jArray.getJSONArray(0); for (int j = 0; j < getpath.length(); j++) { getresult = getpath.getJSONArray(j); _stop.setName(getresult.getString(0)); _map.setLat(getresult.getDouble(1)); _map.setLng(getresult.getDouble(2)); StopElement.Stop_name_list.add(_stop.getName()); MapCoordinatesElement.Lat.add(_map.getLat()); MapCoordinatesElement.Lng.add(_map.getLng()); //Log.d("Stop names", _stop.getName()); //Log.d("Index Array", getpath.toString()); } } } // Log.d("Stop names of Algo", StopElement.Stop_name_list.toString()); // Log.d("Index Stop names", StopElement.Stop_name_list.toString()); } catch (JSONException e) { e.printStackTrace(); }
Delete or remove the polylines in google map api v2
I am trying this from quite long so is why i am here seeking for some serious help!! In my application i am suppose to plot more than 2 alternative routes onclick of a button, say in my application i have 3 buttons such as r1,r2 and r3.When i enter the source and destination locations in my application's respective's edittexts and click enter then i shall be plotting by default r1 so now when i click on r2 button the path/route plotted on the map previously should be erased/deleted and the new alternative path/route should be plotted on the map but isnt happening that way instead of erasing the previous path/route it is plotting both the routes on the map:( can someone please help me? Here is my code. List<RoutesDictionary> routeDicList = new ArrayList<RoutesDictionary>(); routeTwo.setOnClickListener(new OnClickListener() { #Override public void onClick(View arg0) { map.clear(); routeDicList.clear(); routeType = 2; rout = "walking"; new TrafficData().execute(); Log.d("TH routeTwo", "In routeTwo After" + `enter code here`routeDicList.size()); } }); public class TrafficData extends AsyncTask<Context, String, String> { routeTwo.setOnClickListener(new OnClickListener() { #Override public void onClick(View arg0) { map.clear(); routeDicList.clear(); routeType = 2; rout = "walking"; new TrafficData().execute(); } }); #Override protected String doInBackground(Context... params) { try { if (!via_route.equals("")) { url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + sourceAdd + "&destination=" + destinationAdd + "&waypoints=via:" + via_route + "&sensor=false&mode=" + rout; } else { url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + sourceAdd + "&destination=" + destinationAdd + "&sensor=false&mode=" + rout; } // Making HTTP request try { // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpPost = new HttpGet(url); HttpResponse httpResponse = httpClient.execute(httpPost); result = EntityUtils.toString(httpResponse.getEntity()); Log.v("Traffic App : ", "The Traffic App result is : " + result); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } return result; } #Override protected void onPostExecute(String result) { super.onPostExecute(result); Log.v("Traffic App : ", "The Traffic App resultTraffic is : " + resultTraffic); try { JSONObject routesObj = new JSONObject(result); String status = routesObj.getString("status"); if (status.equals("OK")) { routesDictionary = new RoutesDictionary(); allRouteDic.addSrcAddress(sourceAdd); allRouteDic.addDesAddress(destinationAdd); AllRoutes.allRoutes.add(allRouteDic); Log.v("***********************", "THE ***** Array list : "); JSONArray routesArr = routesObj.getJSONArray("routes"); JSONObject mainObj = routesArr.getJSONObject(0); JSONArray legsArr = mainObj.getJSONArray("legs"); int legsSize = legsArr.length(); for (int i = 0; i < legsSize; i++) { routesDictionary.addlegsDistance(legsArr.getJSONObject( i).getJSONObject("distance")); routesDictionary.addlegsDuration(legsArr.getJSONObject( i).getJSONObject("duration")); routesDictionary.addlegsEndaddress(legsArr .getJSONObject(i).getString("end_address")); routesDictionary .addlegsEndlocation(legsArr.getJSONObject(i) .getJSONObject("end_location")); routesDictionary.addlegsStartaddress(legsArr .getJSONObject(i).getString("start_address")); routesDictionary.addlegsStartlocation(legsArr .getJSONObject(i).getJSONObject( "start_location")); routeDicList.add(routesDictionary); JSONArray stepsArr = legsArr.getJSONObject(i) .getJSONArray("steps"); int stepsLen = stepsArr.length(); for (int j = 0; j < stepsLen; j++) { routesDictionary .addlegs_StepsDistance(stepsArr .getJSONObject(j).getJSONObject( "distance")); routesDictionary .addlegs_StepsDuration(stepsArr .getJSONObject(j).getJSONObject( "duration")); routesDictionary.addlegs_StepsEndlocation(stepsArr .getJSONObject(j).getJSONObject( "end_location")); routesDictionary.addlegs_StepsHtmlInstruct(stepsArr .getJSONObject(j).getString( "html_instructions")); routesDictionary .addlegs_StepsPloyline(stepsArr .getJSONObject(j).getJSONObject( "polyline")); routesDictionary .addlegs_StepsStartlocation(stepsArr .getJSONObject(j).getJSONObject( "start_location")); routesDictionary.addlegs_StepsTravelmode(stepsArr .getJSONObject(j).getString("travel_mode")); routeDicList.add(routesDictionary); } } int strLoc = routeDicList.get(0) .getlegs_StepsStartlocation().size(); for (int k = 0; k < strLoc; k++) { routesDictionary.addStrLatit(routeDicList.get(0) .getlegs_StepsStartlocation().get(k) .getString("lat")); routesDictionary.addStrlongi(routeDicList.get(0) .getlegs_StepsStartlocation().get(k) .getString("lng")); routeDicList.add(routesDictionary); } int endLoc = routeDicList.get(0).getlegs_StepsEndlocation() .size(); for (int l = 0; l < endLoc; l++) { routesDictionary.addEndLatit(routeDicList.get(0) .getlegs_StepsEndlocation().get(l) .getString("lat")); routesDictionary.addEndLongi(routeDicList.get(0) .getlegs_StepsEndlocation().get(l) .getString("lng")); routeDicList.add(routesDictionary); } int strL = routeDicList.get(0).getStrLatit().size(); for (int m = 0; m < strL; m++) { Log.v("Traffic : ", "*** Latitude and Longi *** : " + routeDicList.get(0).getStrLatit().get(m) + " " + routeDicList.get(0).getStrLong().get(m)); } int endL = routeDicList.get(0).getEndLatit().size(); for (int m = 0; m < endL; m++) { Log.v("Traffic : ", "### Latitude and Longi ### : " + routeDicList.get(0).getEndLatit().get(m) + " " + routeDicList.get(0).getEndLong().get(m)); } rectOptions = new PolylineOptions(); Log.v("*****************", "The RECTPOINTS SIZE : " + rectOptions.getPoints().size()); rectOptions.getPoints().clear(); switch (routeType) { case 1: rectOptions.color(Color.RED); break; case 2: rectOptions.color(Color.BLUE); break; case 3: rectOptions.color(Color.GREEN); break; default: break; } rectOptions.width(4); for (int i = 1; i < strL; i++) { rectOptions.add(new LatLng(Double.valueOf(routeDicList .get(0).getStrLatit().get(i)), Double .valueOf(routeDicList.get(0).getStrLong() .get(i)))); } // Get back the mutable Polygon map.clear(); polyline = map.addPolyline(rectOptions.geodesic(false)); polyline.remove(); polyline = map.addPolyline(rectOptions.geodesic(true)); double lng = Double.valueOf(routeDicList.get(0) .getlegs_StepsStartlocation().get(0) .getString("lng")); double lat = Double.valueOf(routeDicList.get(0) .getlegs_StepsStartlocation().get(0) .getString("lat")); map.animateCamera(CameraUpdateFactory.newLatLngZoom( new LatLng(lat, lng), 14.0f)); // start longitude and latitude double startlng = Double .valueOf(routeDicList.get(0) .getlegs_StepsEndlocation().get(0) .getString("lng")); double startlat = Double .valueOf(routeDicList.get(0) .getlegs_StepsEndlocation().get(0) .getString("lat")); // start location marker addMarker(startlat, startlng); // end longitude and latitude double endlng = Double.valueOf(routeDicList.get(0) .getlegs_StepsStartlocation().get(endLoc - 1) .getString("lng")); double endlat = Double.valueOf(routeDicList.get(0) .getlegs_StepsStartlocation().get(endLoc - 1) .getString("lat")); // end location marker addMarker(endlat, endlng); } } catch (JSONException e) { e.printStackTrace(); } }
1- You set the OnClickListener for routeTwo button twice one in the main class and one in the AsyncTask , the one in the AsyncTask should be removed. 2- Pressing the button twice fast would produce your problem, because you will start two separably AsyncTasks each one will draw a route so both routes would be displayed, i prefer using just one AsyncTask as a global variable and before running new one check if there is a running AsyncTask ,this could be done by if(trafficData != null && trafficData.getStatus() != AsyncTask.Status.FINISHED){ trafficData .cancel(true); }
json parsing with listview in android?
Categorylist = new ArrayList<CategoryModel>(); try { JSONArray jsonArray = new JSONArray(category()); Log.i(CategoryActivity.class.getName(), "Number of entries " + jsonArray.length()); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); if (jsonObject == null) return null; I try this code, but it gives error please suggest me a best way to do this
Getting String from Json?
I have a strange problem in my android app. In one method I do this : try { String r = responseBody.toString(); JSONArray jArray = new JSONArray(r); categorys = new String[jArray.length()]; idcategory = new Integer[jArray.length()]; System.out.println("lung " + jArray.length()); for (int i = 0; i < jArray.length(); i++) { JSONObject jsdata = jArray.getJSONObject(i); String idcat = jsdata.getString("id_category"); idcategory[i] = Integer.valueOf(idcat); System.out.println("Id " + idcat); String namecategory = jsdata.getString("category_name"); categorys[i] = namecategory; System.out.println("Category name " + namecategory); } and everything works fine, I get from server the categorys and category's id. In an other method I do this (for an other response) : try { String re = responseBody.toString(); JSONArray jArray = new JSONArray(re); System.out.println("lung " + jArray.length()); titlephotos = new String[jArray.length()]; photolink=new String[jArray.length()]; for (int i = 0; i < jArray.length(); i++) { JSONObject jsdata = jArray.getJSONObject(i); String titlephoto = jsdata.getString("title"); System.out.println("titlu photo " + titlephoto); titlephotos[i] = titlephoto; String linkphoto=jsdata.getString("view"); System.out.println("link photo"+ linkphoto); photolink[i]=linkphoto; } }catch(JSONException e) { System.out.println("You are in catch"); } and I get only one title photo an after that I get the message from catch(). If I don't put String linkphoto=jsdata.getString("photo link"); System.out.println("link photo"+ linkphoto); photolink[i]=linkphoto; I get all the titles. I don't understand where is the problem,because the methods are similar, and the first one works fine. Can anyone help? Thanks...
Most likely it is the space in String linkphoto=jsdata.getString("photo link");