I am making an activity in android that fetches the JSON file from a given url. The JSON file will then provide the URL to the video and the image thumbnail which will be displayed in the activity.
But I am not able to fetch the JSON from the hardcoded url that i am passing to the GetData class. The type is GET. Infact i am not even able to run it, the activity crashes the app. Kindly tell me whats the problem in the code.
P.S. I have included the internet permissions.
public class GetData extends AsyncTask<String, Void, Boolean>{
protected Boolean doInBackground(String... url) {
try {
HttpGet httpRequest = null;
httpRequest = new HttpGet(url.toURI());
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
String input = b_entity.getContent().toString();
JSONObject jobj1 = new JSONObject(input).getJSONObject("thumb");
JSONObject jobj2 = new JSONObject(input).getJSONObject("video");
url_thmb=jobj1.getString("thumb");
url_vdo=jobj2.getString("video");
}catch(JSONException e){
Log.e("EX", e.toString());
}catch (URISyntaxException e){
e.printStackTrace();
}catch (MalformedURLException e){
Log.e("log", "bad url");
}catch (IOException e) {
Log.e("log", "io error");
}
return null;
}
the logcat says:
12-12 14:23:15.050: D/dalvikvm(23233): Late-enabling CheckJNI
12-12 14:23:15.170: I/dalvikvm(23233): Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
12-12 14:23:15.170: W/dalvikvm(23233): VFY: unable to resolve virtual method 11338: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
12-12 14:23:15.170: D/dalvikvm(23233): VFY: replacing opcode 0x6f at 0x0000
12-12 14:23:15.170: I/dalvikvm(23233): Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
12-12 14:23:15.170: W/dalvikvm(23233): VFY: unable to resolve virtual method 11344: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
12-12 14:23:15.170: D/dalvikvm(23233): VFY: replacing opcode 0x6f at 0x0000
12-12 14:23:15.170: I/dalvikvm(23233): Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
12-12 14:23:15.170: W/dalvikvm(23233): VFY: unable to resolve virtual method 9032: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
12-12 14:23:15.170: D/dalvikvm(23233): VFY: replacing opcode 0x6e at 0x000e
12-12 14:23:15.190: I/dalvikvm(23233): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
12-12 14:23:15.190: W/dalvikvm(23233): VFY: unable to resolve virtual method 364: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
12-12 14:23:15.190: D/dalvikvm(23233): VFY: replacing opcode 0x6e at 0x0002
12-12 14:23:15.190: I/dalvikvm(23233): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
12-12 14:23:15.190: W/dalvikvm(23233): VFY: unable to resolve virtual method 386: Landroid/content/res/TypedArray;.getType (I)I
12-12 14:23:15.190: D/dalvikvm(23233): VFY: replacing opcode 0x6e at 0x0002
12-12 14:23:15.271: D/AndroidRuntime(23233): Shutting down VM
12-12 14:23:15.271: W/dalvikvm(23233): threadid=1: thread exiting with uncaught exception (group=0x41635d88)
12-12 14:23:15.271: E/AndroidRuntime(23233): FATAL EXCEPTION: main
12-12 14:23:15.271: E/AndroidRuntime(23233): Process: com.example.streamer, PID: 23233
12-12 14:23:15.271: E/AndroidRuntime(23233): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.streamer/com.example.streamer.MainActivityStream}: java.lang.NullPointerException
12-12 14:23:15.271: E/AndroidRuntime(23233): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2237)
12-12 14:23:15.271: E/AndroidRuntime(23233): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
12-12 14:23:15.271: E/AndroidRuntime(23233): at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-12 14:23:15.271: E/AndroidRuntime(23233): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
12-12 14:23:15.271: E/AndroidRuntime(23233): at android.os.Handler.dispatchMessage(Handler.java:102)
12-12 14:23:15.271: E/AndroidRuntime(23233): at android.os.Looper.loop(Looper.java:212)
12-12 14:23:15.271: E/AndroidRuntime(23233): at android.app.ActivityThread.main(ActivityThread.java:5135)
12-12 14:23:15.271: E/AndroidRuntime(23233): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 14:23:15.271: E/AndroidRuntime(23233): at java.lang.reflect.Method.invoke(Method.java:515)
12-12 14:23:15.271: E/AndroidRuntime(23233): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
12-12 14:23:15.271: E/AndroidRuntime(23233): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-12 14:23:15.271: E/AndroidRuntime(23233): at dalvik.system.NativeStart.main(Native Method)
12-12 14:23:15.271: E/AndroidRuntime(23233): Caused by: java.lang.NullPointerException
12-12 14:23:15.271: E/AndroidRuntime(23233): at com.example.streamer.MainActivityStream.onCreate(MainActivityStream.java:64)
12-12 14:23:15.271: E/AndroidRuntime(23233): at android.app.Activity.performCreate(Activity.java:5231)
12-12 14:23:15.271: E/AndroidRuntime(23233): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-12 14:23:15.271: E/AndroidRuntime(23233): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)
12-12 14:23:15.271: E/AndroidRuntime(23233): ... 11 more
12-12 14:23:21.817: E/EX(23233): org.json.JSONException: Value java.io.ByteArrayInputStream@42eeb310 of type java.lang.String cannot be converted to JSONObject
Just modified above code (the one asked in question) by parsing JSON in mainActivity rather than doing it in background and it worked for me.
In mainActivity add:
GetData getData=new GetData();
getData.execute();
String result = null;
try{
result = getData.get().toString();
}catch(Exception e){}
try{
JSONObject jobj1 = new JSONObject(result);
str=jobj1.getString("thumb").toString();
str1=jobj1.getString("video").toString();
}catch(JSONException e){
Log.d("EX", e.toString());
}`
and remove the JSON parsing from getData which earlier I was doing in background.