Parsing JSON data in Android App error
In my Android app , i am trying to get popular media from instagram, i am
using JSONParser class showed in this tutorial
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
public class JSONParser {
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();
HttpGet httpPost = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
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;
}
}
This is the the code i am trying to get to work:
private static String urlInst =
"https://api.instagram.com/v1/media/popular?client_id="+clientId;
public static void Func() {
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(urlInst);
try{
JSONArray pics = json.getJSONArray("data");
for (int i = 0; i < pics.length(); i++) {
JSONObject c=(JSONObject) pics.get(i);
JSONObject user = c.getJSONObject("user");
String name= user.getString("username");
JSONObject img=c.getJSONObject("images");
JSONObject thum=img.getJSONObject("thumbnail");
String urlOfPic = thum.getString("url");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
But i am getting error :
Buffer Error: Error converting result java.lang.NullPointerException
JSON Parser Error parsing data org.json.JSONEXception: End of input at
character 0 of
im trying to figure out what is causing this error, any ideas? As far as i
can see error is in this line
JSONObject json = jParser.getJSONFromUrl(urlInst);
But i am beginner so i am not sure. Help will be apreciated.
No comments:
Post a Comment