I am getting an error in my JSON parser when accessing http://api.rottentomatoes.com/api/public/v1.0/movies/770672123/cast.json?apikey=3p9ehnhzbxwpbd6mk8fncf67.
Here is my JSON Parser it contains a hardcoded string because I was making sure my logic was working and with the hardcoded string I get back the correct data.
package com.apitest.rottentomatoestest; import java.io.*; import org.apache.http.*; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.json.*;
import android.util.Log;
public class JSONParser {
static InputStream inputStream = null; static JSONObject jObject = null; static String jSon = ""; public JSONParser() { // TODO Auto-generated constructor stub } public JSONObject getJSONFromUrl(String url){ //Make HTTP Request try { //defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); inputStream = httpEntity.getContent(); } catch (UnsupportedEncodingException e){ e.printStackTrace(); } catch (ClientProtocolException e){ e.printStackTrace(); }catch (IOException e){ e.printStackTrace(); } try { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); StringBuilder stringBuilder = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null){ stringBuilder.append(line + "\n"); } Log.d("JSON Contents", stringBuilder.toString()); inputStream.close(); jSon = "{\"cast\":[{\"id\":\"162661723\",\"name\":\"Snoop Dogg\"}]}"; } catch (Exception e){ Log.e("Buffer Error", "Error converting result " + e.toString()); } //try to parse the string to JSON Object try { jObject = new JSONObject(jSon); } catch (JSONException e){ Log.e("JSON Parser", "Error parsing data " + e.toString()); } //return JSON String return jObject; }
}
the logcat returns JSON Contents <h1>596 Service Not Found</h1>
Here is my main file as well:
package com.apitest.rottentomatoestest;
import android.os.Bundle;
import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject;
import android.app.Activity; import android.util.Log; import android.view.Menu;
import android.widget.EditText;
public class Main extends Activity {
private static String url = "http://api.rottentomatoes.com/api/public/v1.0/movies/770672123/cast.json?apikey=3p9ehnhzbxwpbd6mk8fncf67"; private static final String TAG_CAST = "cast"; private static final String TAG_NAME = "name"; private static final String TAG_ID = "id"; EditText display; @Override public void onCreate(Bundle savedInstanceState) { try { super.onCreate(savedInstanceState); setContentView(R.layout.mainlayout); JSONArray cast = null; JSONParser jParser = new JSONParser(); JSONObject jSon = jParser.getJSONFromUrl(url); display = (EditText) findViewById(R.id.display); try { cast = jSon.getJSONArray(TAG_CAST); for(int i=0; i < cast.length(); i++){ JSONObject c = cast.getJSONObject(i); String name = c.getString(TAG_NAME); display.setText(name); } } catch (JSONException e) { e.printStackTrace(); } } catch (Exception e){ Log.e("ERROR", "ERROR IN CODE" + e.toString()); e.printStackTrace(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.mainlayout, menu); return true; }
Message edited by Rory Garcia 9 months ago
Steve N. – 9 months ago
Hi Rory,
You want to use an HttpGet, not an HttpPost.
-Steve
Please sign in to add a post.
I am getting an error in my JSON parser when accessing http://api.rottentomatoes.com/api/public/v1.0/movies/770672123/cast.json?apikey=3p9ehnhzbxwpbd6mk8fncf67.
Here is my JSON Parser it contains a hardcoded string because I was making sure my logic was working and with the hardcoded string I get back the correct data.
import android.util.Log;
public class JSONParser {
}
the logcat returns JSON Contents <h1>596 Service Not Found</h1>
Here is my main file as well:
package com.apitest.rottentomatoestest;
import android.os.Bundle;
import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject;
import android.app.Activity; import android.util.Log; import android.view.Menu;
import android.widget.EditText;
public class Main extends Activity {
}
Message edited by Rory Garcia 9 months ago
Tags
Steve N. – 9 months ago
Hi Rory,
You want to use an HttpGet, not an HttpPost.
-Steve