我正在 android 中进行一项从给定 url 获取 JSON 文件的活动。JSON 文件将提供视频的 URL 和将在活动中显示的图像缩略图。
但我无法从我传递给 GetData 类的硬编码 url 中获取 JSON。类型是 GET。事实上,我什至无法运行它,该活动使应用程序崩溃。请告诉我代码中有什么问题。
PS我已经包括了互联网权限。
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;
}
logcat 说:
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