下载文件httppost时出错

时间:2013-01-01 14:56:39

标签: android file download

我需要从网上下载一个json文件。首先,我想说我使用的链接是有效的。 这是代码:

private String getJsonFromUrl(String url) {
        String json = null;

        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            json = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // return json
        return json;
    }

问题是我收到错误:

01-01 15:52:45.130: E/AndroidRuntime(6303): FATAL EXCEPTION: main
01-01 15:52:45.130: E/AndroidRuntime(6303): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.player/com.example.player.PlaySongActivity}: android.os.NetworkOnMainThreadException
01-01 15:52:45.130: E/AndroidRuntime(6303):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2185)
01-01 15:52:45.130: E/AndroidRuntime(6303):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210)
01-01 15:52:45.130: E/AndroidRuntime(6303):     at android.app.ActivityThread.access$600(ActivityThread.java:142)
01-01 15:52:45.130: E/AndroidRuntime(6303):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
01-01 15:52:45.130: E/AndroidRuntime(6303):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 15:52:45.130: E/AndroidRuntime(6303):     at android.os.Looper.loop(Looper.java:137)
01-01 15:52:45.130: E/AndroidRuntime(6303):     at android.app.ActivityThread.main(ActivityThread.java:4931)
01-01 15:52:45.130: E/AndroidRuntime(6303):     at java.lang.reflect.Method.invokeNative(Native Method)
01-01 15:52:45.130: E/AndroidRuntime(6303):     at java.lang.reflect.Method.invoke(Method.java:511)
01-01 15:52:45.130: E/AndroidRuntime(6303):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
01-01 15:52:45.130: E/AndroidRuntime(6303):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
01-01 15:52:45.130: E/AndroidRuntime(6303):     at dalvik.system.NativeStart.main(Native Method)
01-01 15:52:45.130: E/AndroidRuntime(6303): Caused by: android.os.NetworkOnMainThreadException
01-01 15:52:45.130: E/AndroidRuntime(6303):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
01-01 15:52:45.130: E/AndroidRuntime(6303):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
01-01 15:52:45.130: E/AndroidRuntime(6303):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
01-01 15:52:45.130: E/AndroidRuntime(6303):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
01-01 15:52:45.130: E/AndroidRuntime(6303):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
01-01 15:52:45.130: E/AndroidRuntime(6303):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-01 15:52:45.130: E/AndroidRuntime(6303):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-01 15:52:45.130: E/AndroidRuntime(6303):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-01 15:52:45.130: E/AndroidRuntime(6303):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-01 15:52:45.130: E/AndroidRuntime(6303):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-01 15:52:45.130: E/AndroidRuntime(6303):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-01 15:52:45.130: E/AndroidRuntime(6303):     at com.example.player.DBManagement.LastFM.getJsonFromUrl(LastFM.java:243)

1 个答案:

答案 0 :(得分:1)

NetworkOnMainThreadException - 您无法在主UI线程上运行网络内容。您需要启动一个新线程来运行您的httpPost,否则可以通过摆弄严格模式来解决。搜索堆栈溢出。这个问题有很多答案。这是一个答案 avoiding NetworkOnMainThreadException