无法从Android应用程序中的服务器获取json数据

时间:2014-12-12 08:43:50

标签: android json getjson http-get background-task

我在android中创建一个从给定url获取JSON文件的活动。然后,JSON文件将提供视频的URL和将在活动中显示的图像缩略图。

但是我无法从传递给GetData类的硬编码url中获取JSON。类型是GET。事实上,我甚至无法运行它,活动崩溃了应用程序。请告诉我代码中的问题是什么。

P.S。我已经包含了互联网权限。

    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

3 个答案:

答案 0 :(得分:0)

试试这个。

// Create new Socket
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
// Start streaming
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
// Initialize Line
String line = null;
// Start reading
while ((line = reader.readLine()) != null)
{
    JSONObject reader = new JSONObject(line);
    url_thmb = reader.getString("thumb");
    url_vdo = reader.getString("video");
}

编辑:

这是不可能的,因为我刚刚运行该代码,输出如下。检查您的Json文件或代码。

输出是:

12-12 18:27:41.433: I/Choreographer(12476): Skipped 58 frames!  The application may be doing too much work on its main thread.
12-12 18:27:48.300: I/System.out(12476): URL_TMB = 01 URL_VDO = 001
12-12 18:27:52.355: I/System.out(12476): URL_TMB = 02 URL_VDO = 002
12-12 18:27:53.472: I/System.out(12476): URL_TMB = 03 URL_VDO = 003
12-12 18:27:54.394: I/System.out(12476): URL_TMB = 04 URL_VDO = 004
12-12 18:27:55.394: I/System.out(12476): URL_TMB = 05 URL_VDO = 005
12-12 18:27:56.324: I/System.out(12476): URL_TMB = 06 URL_VDO = 006
12-12 18:27:59.371: I/System.out(12476): URL_TMB = 07 URL_VDO = 007
12-12 18:28:00.316: I/System.out(12476): URL_TMB = 08 URL_VDO = 008
12-12 18:28:01.269: I/System.out(12476): URL_TMB = 09 URL_VDO = 009
12-12 18:28:11.105: W/jdwp(12476): Debugger is telling the VM to exit with code=1
12-12 18:28:11.105: I/dalvikvm(12476): GC lifetime allocation: 7948 bytes

编辑2: 这是另一个测试输出。 (我必须将xyz更改为示例,因为SOF测试编辑器不允许我使用它。)

12-13 02:14:45.711: I/System.out(18424): url_thmb = http://example.com/intern/ajnk/thumb.jpg url_vdo = http://example.com/intern/ajnk/vid.mp4
12-13 02:14:46.610: D/lights(273): set_light_backlight: brightness=20
12-13 02:14:49.640: I/ActivityManager(273): Start proc com.sonyericsson.appshare for service com.sonyericsson.appshare/.backend.synchronization.SyncService: pid=18529 uid=10024 gids={3003, 1015}
12-13 02:14:49.750: D/dalvikvm(17648): GC_EXPLICIT freed 319K, 50% free 3396K/6791K, external 1685K/2133K, paused 111ms

答案 1 :(得分:0)

错误意味着您没有获得任何JSON,我建议您记录您的数据。

String input = b_entity.getContent().toString();
Log.d("log_tag", "input = " + input.toString());
JSONObject jobj1 = new JSONObject(input);
Log.d("log_tag", "json = " + jobj1.toString());

答案 2 :(得分:0)

通过在mainActivity中解析JSON而不是在后台执行它来修改上面的代码(问题所讨论的代码),它对我有效。

在mainActivity中添加:

    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());
    }`

并从getData中删除JSON解析,这是我之前在后台进行的。