hello guys.. this is JSONhelper class where i m parsing theurl and
finally in some another class i have written the follwing lines to get
the output
JSONhelper jsoNhelper=new JSONhelper(getApplicationContext());
jsoNhelper.getJson();
ArrayList<PlaceInformation> a=jsoNhelper.resultArrayList;
Log.v("First", " arrayList length="+ a.size());
but above lines show the length as zero... pls check the parser and
let me know where i m wrong. or pls provide the code to me..needed
************************
package com.example;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
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.content.Context;
import android.util.Log;
public class JSONhelper {
//private final String TAG = "JSONhelper";
// private final String API_KEY =
"ABQIAAAAtyg3DZbOZx4k8yKBrp80RBS4tlSElwKHfak6B1dJPxb7rRmHMRT1HQ77dOcnasJbwsK6tpUyy_thEg";
// private final String
// URL="http://174.120.32.69/~mobadmin/clients/milehigh/api/
fetch_data/all/yes";
// private final String baseURL =
// "http://174.120.32.69/~mobadmin/clients/milehigh/api/fetch_data/
all/no/since/";
private final String baseURL = "http://ajax.googleapis.com/ajax/
services/search/local?v=1.0&sll=48.8565,2.3509&q=restaurant";
// private final String HEADER = "x-api-key";
JSONArray resultsJsonArray;
JSONObject placesJsonObject;
ArrayList<PlaceInformation> resultArrayList = new
ArrayList<PlaceInformation>();
// UpdateSharedPreferences updateSharedPreferences;
// public ArrayList<MFShow> showArrayListFromJSON = new
ArrayList<MFShow>();
// public ArrayList<MFArtist> artistArrayListFromJSON = new
// ArrayList<MFArtist>();
// public ArrayList<MFStage> stageArrayListFromJSON = new
// ArrayList<MFStage>();
public JSONhelper(Context context) {
// updateSharedPreferences = new
UpdateSharedPreferences(context);
}
public void getJson() {
HttpGet httpGet = getHttpUriRequest();
HttpClient httpClient = getHttpClient();
HttpResponse httpResponse = getHttpResponse(httpGet,
httpClient);
JSONObject jsonObject = getJSONObject(httpResponse);
// initialiseArrayLists(jsonObject);
getResultsArrayList(jsonObject);
// Log.v(TAG, "ArrayList initialized=" + flag);
}
// String getURL(String since) {
//
// String Url = null;
// Url = baseURL + since;
//
// return Url;
//
// }
public void getResultsArrayList(JSONObject jsonObject) {
try {
resultsJsonArray = jsonObject.getJSONArray("resuslt");
for (int i = 0; i < resultsJsonArray.length(); i++) {
placesJsonObject =
resultsJsonArray.getJSONObject(i);
getPlaceInformation(placesJsonObject);
}
} catch (Exception e) {
// TODO: handle exception
}
}
private void getPlaceInformation(JSONObject placesJsonObject2)
throws JSONException {
PlaceInformation placeinfo = new PlaceInformation();
placeinfo.lat = placesJsonObject2.getString("lat");
placeinfo.lng = placesJsonObject2.getString("lng");
placeinfo.title = placesJsonObject2.getString("title");
placeinfo.city = placesJsonObject2.getString("city");
placeinfo.country = placesJsonObject2.getString("country");
placeinfo.streetAddress =
placesJsonObject2.getString("streetAddress");
placeinfo.addressLines1 = placesJsonObject2
.getJSONArray("addressLines").getString(0);
placeinfo.addressLines2 = placesJsonObject2
.getJSONArray("addressLines").getString(1);
placeinfo.phoneNumbers =
placesJsonObject2.getJSONArray("phoneNumbers")
.getJSONObject(0).getString("phoneNumbers");
resultArrayList.add(placeinfo);
}
// public void getResults(JSONObject jsonObject)
// public ArrayList<MFShow> getShowArrayListFromJSON() {
// return showArrayListFromJSON;
// }
//
// public ArrayList<MFArtist> getArtistArrayListFromJSON() {
// return artistArrayListFromJSON;
// }
//
// public ArrayList<MFStage> getStageArrayListFromJSON() {
// return stageArrayListFromJSON;
// }
private HttpGet getHttpUriRequest() {
HttpGet get = null;
Log.v("111", "tt" + get);
String URL = baseURL;// getURL(("since"));
try {
get = new HttpGet(URL);
Log.v("111", "tt" + get);
Log.v("111", "tt" + URL);
} catch (IllegalArgumentException e) {
// Log.v(TAG, "IllegalArgumentException");
e.printStackTrace();
}
// get.addHeader(HEADER, API_KEY);
return get;
}
private HttpClient getHttpClient() {
HttpClient httpClient = new DefaultHttpClient();
Log.v("222", "ccc" + httpClient);
return httpClient;
}
private HttpResponse getHttpResponse(HttpGet get, HttpClient
httpClient) {
HttpResponse httpResponse = null;
try {
Log.v("getHttpResponse====", "httpClient" + httpClient);
Log.v("getHttpResponse====", "get" + get);
httpResponse = httpClient.execute(get);
Log.v("333", "rrr" + httpResponse);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
// Log.v(TAG, "IOException");
e.printStackTrace();
}
return httpResponse;
}
JSONObject getJSONObject(HttpResponse httpResponse) {
String jsonString = null;
JSONObject responseDataJsonObject = null;
if (httpResponse.getStatusLine().toString().equals("HTTP/1.1 200
OK"))
{
try {
jsonString =
EntityUtils.toString(httpResponse.getEntity());
Log.v("sdfvsd", "*********" +
jsonString.toString());
responseDataJsonObject = new
JSONObject(jsonString);
} catch (ParseException e) {
// Log.v(TAG, "ParseException");
e.printStackTrace();
} catch (IOException e) {
// Log.v(TAG, "IOException");
e.printStackTrace();
} catch (JSONException e) {
// Log.v(TAG, "JSONException");
e.printStackTrace();
}
}
return responseDataJsonObject;
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en