android - 获取远程JSON抛出错误

时间:2011-07-31 18:02:13

标签: java android eclipse json httpclient

我正在尝试加载远程文件,我必须使用始终抛出错误的HttpClient.execute。有什么想法吗?

我正在使用的功能:

private void loadCol3(Map.Entry<String, String> map) {
    LinearLayout col3 = (LinearLayout)findViewById(R.id.col3);
    String result = null;
    col3.removeAllViews();

    Log.d(TAG, "start load col 3");

    HttpClient httpclient = new DefaultHttpClient();
    HttpGet request = new HttpGet("http://video.foxbusiness.com/v/feed/playlist/"+map.getValue()+".js?template=fox&callback=?");
    ResponseHandler<String> handler = new BasicResponseHandler();  

    try {  
        result = httpclient.execute(request, handler);  
    } catch (Exception e) {
        Log.d(TAG, "httpclient.execute failed");
        e.printStackTrace();  
    }
    httpclient.getConnectionManager().shutdown();

    try {
        JSONObject completeJSONObj = new JSONObject(result);
        String json = completeJSONObj.toString();
        Log.d(TAG, json);
        JSONObject results = completeJSONObj.getJSONObject("results");
    } catch (JSONException e) {
        Log.d(TAG,"JSON parsing error:" + e.getMessage());
    }
}

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.tv"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="11" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <activity android:name=".MyActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

运行时的当前日志:

07-31 17:56:17.175: DEBUG/log_tag(10869): start load col 3
07-31 17:56:17.205: DEBUG/log_tag(10869): httpclient.execute failed

最后一行控制台:

[2011-07-31 11:57:57 - My app] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.tv/.MyActivity }

**编辑 - stacktraces **

07-31 18:11:47.475: WARN/System.err(11083): android.os.NetworkOnMainThreadException
07-31 18:11:47.475: WARN/System.err(11083):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
07-31 18:11:47.475: WARN/System.err(11083):     at java.net.InetAddress.lookupHostByName(InetAddress.java:477)
07-31 18:11:47.475: WARN/System.err(11083):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:277)
07-31 18:11:47.475: WARN/System.err(11083):     at java.net.InetAddress.getAllByName(InetAddress.java:249)
07-31 18:11:47.475: WARN/System.err(11083):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
07-31 18:11:47.475: WARN/System.err(11083):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-31 18:11:47.475: WARN/System.err(11083):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-31 18:11:47.475: WARN/System.err(11083):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-31 18:11:47.475: WARN/System.err(11083):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-31 18:11:47.475: WARN/System.err(11083):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
07-31 18:11:47.475: WARN/System.err(11083):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
07-31 18:11:47.475: WARN/System.err(11083):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
07-31 18:11:47.475: WARN/System.err(11083):     at com.foxbusiness.tv.FOXBusinessActivity.loadCol3(FOXBusinessActivity.java:134)
07-31 18:11:47.475: WARN/System.err(11083):     at com.foxbusiness.tv.FOXBusinessActivity.access$1(FOXBusinessActivity.java:121)
07-31 18:11:47.475: WARN/System.err(11083):     at com.foxbusiness.tv.FOXBusinessActivity$2.onClick(FOXBusinessActivity.java:113)
07-31 18:11:47.475: WARN/System.err(11083):     at android.view.View.performClick(View.java:3110)
07-31 18:11:47.475: WARN/System.err(11083):     at android.view.View$PerformClick.run(View.java:11928)
07-31 18:11:47.485: WARN/System.err(11083):     at android.os.Handler.handleCallback(Handler.java:587)
07-31 18:11:47.485: WARN/System.err(11083):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-31 18:11:47.485: WARN/System.err(11083):     at android.os.Looper.loop(Looper.java:132)
07-31 18:11:47.485: WARN/System.err(11083):     at android.app.ActivityThread.main(ActivityThread.java:4025)
07-31 18:11:47.485: WARN/System.err(11083):     at java.lang.reflect.Method.invokeNative(Native Method)
07-31 18:11:47.485: WARN/System.err(11083):     at java.lang.reflect.Method.invoke(Method.java:491)
07-31 18:11:47.485: WARN/System.err(11083):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-31 18:11:47.485: WARN/System.err(11083):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-31 18:11:47.485: WARN/System.err(11083):     at dalvik.system.NativeStart.main(Native Method)

07-31 18:09:26.915: ERROR/AndroidRuntime(11024): FATAL EXCEPTION: main
07-31 18:09:26.915: ERROR/AndroidRuntime(11024): java.lang.NullPointerException
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:112)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at org.json.JSONTokener.nextValue(JSONTokener.java:90)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at org.json.JSONObject.<init>(JSONObject.java:154)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at org.json.JSONObject.<init>(JSONObject.java:171)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at com.example.tv.MyActivity.loadCol3(MyActivity.java:142)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at com.example.tv.MyActivity.access$1(MyActivity.java:121)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at com.example.tv.MyActivity$2.onClick(MyActivity.java:113)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at android.view.View.performClick(View.java:3110)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at android.view.View$PerformClick.run(View.java:11928)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at android.os.Handler.handleCallback(Handler.java:587)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at android.os.Looper.loop(Looper.java:132)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at android.app.ActivityThread.main(ActivityThread.java:4025)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at java.lang.reflect.Method.invokeNative(Native Method)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at java.lang.reflect.Method.invoke(Method.java:491)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-31 18:09:26.915: ERROR/AndroidRuntime(11024):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:3)

您获得NullpointerException,因为即使GET方法失败,您也可以访问result

对于NetworkOnMainThreadException,问题恰恰如此:你在主线程上进行网络连接。在AsyncTaskIntentService中运行您的GET请求。