gpt4 book ai didi

java - Android 代码在 Gingerbread 上运行良好,但在 Nexus 7 w/4.2.2 上崩溃

转载 作者:行者123 更新时间:2023-12-01 17:24:09 25 4
gpt4 key购买 nike

我有 2 台设备。带 Gingerbread 的 Droid x3 和带 Jellybean 4.2.2 的 Nexus 7。我在我的 Droid x3 上编译并运行了这段代码,它运行得很好。然而,我在 Nexus 7 上运行的完全相同的代码却崩溃了。

包括代码和堆栈跟踪。

感谢您的帮助!

package com.jacobschellenbergflickrsearch;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class DataDownloader {

String apiKey = "bc370c6386192bf6e2f950cdfddfda48";
//String url = "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=bc370c6386192bf6e2f950cdfddfda48&nojsoncallback=1&text=monkey&format=json&per_page=10";


public String buildUrl(String searchMethod, String searchQuery, int perpage){

String url = String.format("http://api.flickr.com/services/rest/?method=%s&api_key=%s&nojsoncallback=1&text=%s&format=json&per_page=%s", searchMethod, this.apiKey, searchQuery, perpage);

String result = null;

try{
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(request);
InputStream ips = response.getEntity().getContent();
BufferedReader buf = new BufferedReader(new InputStreamReader(ips,"UTF-8"));

StringBuilder sb = new StringBuilder();
String s;

while(true)
{
s = buf.readLine();
if(s==null || s.length()==0){
break;
}
sb.append(s);
}
buf.close();
ips.close();
result = sb.toString();
}
catch (IllegalStateException e) {
e.printStackTrace();
result = "IllegalStateException";
}
catch (IOException e) {
e.printStackTrace();
result = "IOException";
} catch (URISyntaxException e) {
e.printStackTrace();
result = "URISyntaxException";
}

return "Query: " + url + " : Result: " + result;
}
}

05-01 13:47:04.742: D/AndroidRuntime(4526): Shutting down VM 05-01 13:47:04.742: W/dalvikvm(4526): threadid=1: thread exiting with uncaught exception (group=0x41377930) 05-01 13:47:04.752: E/AndroidRuntime(4526): FATAL EXCEPTION: main 05-01 13:47:04.752: E/AndroidRuntime(4526): java.lang.IllegalStateException: Could not execute method of the activity 05-01 13:47:04.752: E/AndroidRuntime(4526): at android.view.View$1.onClick(View.java:3599) 05-01 13:47:04.752: E/AndroidRuntime(4526): at android.view.View.performClick(View.java:4204) 05-01 13:47:04.752: E/AndroidRuntime(4526): at android.view.View$PerformClick.run(View.java:17355) 05-01 13:47:04.752: E/AndroidRuntime(4526): at android.os.Handler.handleCallback(Handler.java:725) 05-01 13:47:04.752: E/AndroidRuntime(4526): at android.os.Handler.dispatchMessage(Handler.java:92) 05-01 13:47:04.752: E/AndroidRuntime(4526): at android.os.Looper.loop(Looper.java:137) 05-01 13:47:04.752: E/AndroidRuntime(4526): at android.app.ActivityThread.main(ActivityThread.java:5041) 05-01 13:47:04.752: E/AndroidRuntime(4526): at java.lang.reflect.Method.invokeNative(Native Method) 05-01 13:47:04.752: E/AndroidRuntime(4526): at java.lang.reflect.Method.invoke(Method.java:511) 05-01 13:47:04.752: E/AndroidRuntime(4526): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 05-01 13:47:04.752: E/AndroidRuntime(4526): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 05-01 13:47:04.752: E/AndroidRuntime(4526): at dalvik.system.NativeStart.main(Native Method) 05-01 13:47:04.752: E/AndroidRuntime(4526): Caused by: java.lang.reflect.InvocationTargetException 05-01 13:47:04.752: E/AndroidRuntime(4526): at java.lang.reflect.Method.invokeNative(Native Method) 05-01 13:47:04.752: E/AndroidRuntime(4526): at java.lang.reflect.Method.invoke(Method.java:511) 05-01 13:47:04.752: E/AndroidRuntime(4526): at android.view.View$1.onClick(View.java:3594) 05-01 13:47:04.752: E/AndroidRuntime(4526): ... 11 more 05-01 13:47:04.752: E/AndroidRuntime(4526): Caused by: android.os.NetworkOnMainThreadException 05-01 13:47:04.752: E/AndroidRuntime(4526): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 05-01 13:47:04.752: E/AndroidRuntime(4526): at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 05-01 13:47:04.752: E/AndroidRuntime(4526): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 05-01 13:47:04.752: E/AndroidRuntime(4526): at java.net.InetAddress.getAllByName(InetAddress.java:214) 05-01 13:47:04.752: E/AndroidRuntime(4526): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 05-01 13:47:04.752: E/AndroidRuntime(4526): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 05-01 13:47:04.752: E/AndroidRuntime(4526): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 05-01 13:47:04.752: E/AndroidRuntime(4526): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 05-01 13:47:04.752: E/AndroidRuntime(4526): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 05-01 13:47:04.752: E/AndroidRuntime(4526): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 05-01 13:47:04.752: E/AndroidRuntime(4526): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 05-01 13:47:04.752: E/AndroidRuntime(4526): at com.jacobschellenbergflickrsearch.DataDownloader.buildUrl(DataDownloader.java:31) 05-01 13:47:04.752: E/AndroidRuntime(4526): at com.jacobschellenbergflickrsearch.MainActivity.searchImage(MainActivity.java:47) 05-01 13:47:04.752: E/AndroidRuntime(4526): ... 14 more 05-01 13:47:04.802: D/dalvikvm(4526): GC_CONCURRENT freed 210K, 5% free 7474K/7812K, paused 5ms+2ms, total 46ms

最佳答案

Caused by: android.os.NetworkOnMainThreadException

您正在主应用程序线程上执行网络 I/O。这在任何版本的 Android 上都是一个非常糟糕的主意,并且在 Android 4.0+ 上会引发实际异常。

请在后台线程上执行网络 I/O,例如使用 AsyncTask

关于java - Android 代码在 Gingerbread 上运行良好,但在 Nexus 7 w/4.2.2 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16326279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com