我需要从web下载一个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)
NetworkOnMainThreadException
-你不能在主UI线程上运行你的网络东西。您需要启动一个新线程来运行httpPost,或者通过使用严格模式来解决问题。搜索堆栈溢出。这个问题有很多答案。这里有一个答案避免networkonmainthreadeexception