hi guys this the class where i m parsing the values
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 baseURL = "http://ajax.googleapis.com/ajax/
services/search/local?v=1.0&sll=48.8565,2.3509&q=restaurant";
JSONArray resultsJsonArray;
JSONObject placesJsonObject;
ArrayList<PlaceInformation> resultArrayList = new
ArrayList<PlaceInformation>();
public JSONhelper(Context 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);
}
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);
}
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();
}
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;
}
}
*********************
later in next class i m
package com.example;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class First extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
JSONhelper jsoNhelper=new JSONhelper(getApplicationContext());
jsoNhelper.getJson();
ArrayList<PlaceInformation> a=jsoNhelper.resultArrayList;
Log.v("First", " arrayList length="+ a.size());
}
}
here i m not getting the list of array...means the out put....
pls comment and solve my issues
--
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