Android parsing json returns exception-Collection of common programming errors
Janusz ChudzynskiI am trying to parse a JSON using the JSONParser class presented below.
Based on breakpoints it seems like it’s crashing on this line of code:
HttpResponse httpResponse = httpClient.execute(httpPost);
I don’t know how to proceed with it. I will appreciate any help.
FRAGMENT
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.agenda, container, false); final ListView listview = (ListView)view.findViewById(R.id.listView); String[] values = new String[] {"Summit 2013","Sponsors","Agenda","Survey","Website","Credits" }; Log.v(TAG,"Created"); String[] array = new String[10]; array[0] = ""; **new RetrieveFeedTask().doInBackground("");**
Async Task
class RetrieveFeedTask extends AsyncTask { private Exception exception; protected JSONObject doInBackground(String... params) { try { // URL url= new URL(urls[0]); JSONParser parser = new JSONParser(); JSONObject jobject = parser.getJSONFromUrl("http://www.itenwired.com/json"); return jobject; } catch (Exception e) { this.exception = e; Log.v(TAG,"Exception"+exception.getLocalizedMessage()); return null; } } protected void onPostExecute(JSONObject result) { Log.v(TAG,"Downloaded"); Log.v(TAG,result.toString()); } }
JSON PARSER
package com.iten.itenwired2013; /** * Created by jmc72 on 7/10/13. */ 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.client.ClientProtocolException; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONException; import org.json.JSONObject; import org.apache.http.client.HttpResponseException; import android.util.Log; public class JSONParser { private static final String TAG = "JSON PARSER"; static InputStream is = null; static JSONObject jObj = null; static String json = ""; // constructor public JSONParser() { } public JSONObject getJSONFromUrl(String url) { // Making HTTP request try { // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); // Log.v(TAG, httpPost.getURI().toString()); //new HttpPost(); httpPost.setHeader("Content-type", "application/json"); Log.v(TAG, "01"); HttpResponse httpResponse = httpClient.execute(httpPost); Log.v(TAG, "Aaa " + httpResponse.toString()); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); Log.v(TAG, "1"); } catch (HttpResponseException e) { Log.v(TAG, "000"); } catch (UnsupportedEncodingException e) { Log.v(TAG, "2"); e.printStackTrace(); } catch (ClientProtocolException e) { Log.v(TAG, "3"); e.printStackTrace(); } catch (IOException e) { Log.v(TAG, "4"); e.printStackTrace(); } 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(); json = sb.toString(); } catch (Exception e) { Log.e("Buffer Error", "Error converting result " + e.toString()); } // try parse the string to a JSON object try { jObj = new JSONObject(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } // return JSON String return jObj; } }
LOG CAT
07-12 14:18:19.713 322-656/system_process W/ActivityManager: Unbind failed: could not find connection for android.os.BinderProxy@40d233c0 07-12 14:18:20.953 1304-1304/? D/AndroidRuntime: >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit > AndroidRuntime START com.android.internal.os.RuntimeInit