Reputation: 14838
Hello again this is my second question in SO. This is my first question. The answer given by the guys are really helpful so i tried to implements it.And here is my implementation..
Making a JSONFunction class with a static method whcch will return the json Object.
public class JSONFunction {
public static JSONObject getJSONfromURL(String url){
InputStream is = null;
String result = "";
JSONObject jArray = null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
jArray = new JSONObject(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return jArray;
}
}
NOW MY MAIN ACTIVITY is..
public class JsonExampleActivity extends ListActivity {
/** Called when the activity is first created. */
JSONFunction JSONfunction;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONFunction.getJSONfromURL("https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=1000&types=resturants&sensor=false&key=0bBgLl42nWwl7TQHrAFpY89v2FeLlijIGTLJ1AA");
try{
JSONObject httpattributr= json.getJSONObject("html_attributions");
//JSONObject results =new JSONObject("results");
JSONArray JArray = httpattributr.getJSONArray("results");
for(int i=0;i<JArray.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = JArray.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name", "" + e.getString("name"));
map.put("type", "type: " + e.getString("type"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
new String[] { "name", "type" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(JsonExampleActivity.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
}
}
But i am getting error in logcat as
11-04 19:56:53.099: ERROR/log_tag(298): Error parsing data org.json.JSONException: JSONObject["html_attributions"] is not a JSONObject.
Any help would be highly appreciated
Thanks in advance.
The JSON result is look like
{
"html_attributions" : [
"Listings by \u003ca href=\"http://www.yellowpages.com.au/\"\u003eYellow Pages\u003c/a\u003e"
],
"results" : [
{
"geometry" : {
"location" : {
"lat" : -33.8719830,
"lng" : 151.1990860
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id" : "677679492a58049a7eae079e0890897eb953d79b",
"name" : "Zaaffran Restaurant - BBQ and GRILL, Darling Harbour",
"rating" : 3.90,
"reference" : "CpQBjAAAAHDHuimUQATR6gfoWNmZlk5dKUKq_n46BpSzPQCjk1m9glTKkiAHH_Gs4xGttdOSj35WJJDAV90dAPnNnZK2OaxMgogdeHKQhIedh6UduFrW53wtwXigUfpAzsCgIzYNI0UQtCj38cr_DE56RH4Wi9d2bWbbIuRyDX6tx2Fmk2EQzO_lVJ-oq4ZY5uI6I75RnxIQJ6smWUVVIHup9Jvc517DKhoUidfNPyQZZIgGiXS_SwGQ1wg0gtc",
"types" : [ "restaurant", "food", "establishment" ],
"vicinity" : "Harbourside Centre 10 Darling Drive, Darling Harbour, Sydney"
},
Upvotes: 0
Views: 1863
Reputation: 10938
Change
JSONObject httpattributr= json.getJSONObject("html_attributions");
JSONArray JArray = httpattributr.getJSONArray("results");
to
JSONArray httpattributr= json.getJSONArray("html_attributions");
JSONArray JArray = json.getJSONArray("results");
EDIT: The result array isn't a child of or element in the html_attributions array - they are siblings.
Upvotes: 0
Reputation: 29141
"html_attributions" key has JSONArray as it's value, so instead of
JSONObject httpattributr= json.getJSONObject("html_attributions");
try using...
JSONArray httpattributtr = json.getJSONArray("html_attributions");
Upvotes: 1
Reputation: 30845
In the json that you're parsing, there must not be a key named "html_attributions". What does your json look like?
Upvotes: 0