我有 2 台设备。Droid x3 w/ Gingerbread 和 Nexus 7 w/ Jellybean 4.2.2。我在我的 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): 关闭 VM 05-01 13:47:04.742: W/dalvikvm(4526): threadid=1: 线程退出未捕获异常 (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: 无法执行活动的方法 05- 01 13:47:04.752: E/AndroidRuntime(4526): 在 android.view.View$1.onClick(View.java:3599) 05-01 13:47:04.752: E/AndroidRuntime(4526): 在 android.view .View.performClick(View.java:4204) 05-01 13:47:04.752: E/AndroidRuntime(4526): 在 android.view.View$PerformClick.run(View.java:17355) 05-01 13:47 :04.752: E/AndroidRuntime(4526): 在 android.os.Handler.handleCallback(Handler.java:725) 05-01 13:47:04.752: E/AndroidRuntime(4526): 在 android.os.Handler。dispatchMessage(Handler.java:92) 05-01 13:47:04.752: E/AndroidRuntime(4526): 在 android.os.Looper.loop(Looper.java:137) 05-01 13:47:04.752: E/ AndroidRuntime(4526): 在 android.app.ActivityThread.main(ActivityThread.java:5041) 05-01 13:47:04.752: E/AndroidRuntime(4526): 在 java.lang.reflect.Method.invokeNative(Native Method) 05-01 13:47:04.752: E/AndroidRuntime(4526): 在 java.lang.reflect.Method.invoke(Method.java:511) 05-01 13:47:04.752: E/AndroidRuntime(4526): 在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经过:java.lang.reflect.InvocationTargetException 05-01 13:47:04.752: E/AndroidRuntime(4526): 在 java.lang.reflect.Method.invokeNative(Native Method) 05-01 13:47:04.752: E/AndroidRuntime( 4526): 在 java.lang.reflect.Method.invoke(Method.java:511) 05-01 13:47:04.752: E/AndroidRuntime(4526): 在 android.view.View$1.onClick(View.java: 3594) 05-01 13:47:04.752: E/AndroidRuntime(4526): ... 11 更多 05-01 13:47:04.752: E/AndroidRuntime(4526): 引起:android.os.NetworkOnMainThreadException 05-01 13:47:04.752: E/AndroidRuntime(4526): 在 android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 05-01 13:47:04.752: E/AndroidRuntime(4526): 在 java.net .InetAddress.lookupHostByName(InetAddress.java:385) 05-01 13:47:04.752: E/AndroidRuntime(4526): 在 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): 在 org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 05-01 13:47:04.752: E/AndroidRuntime(4526): 在 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): 在 org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 05-01 13:47:04.752: E/AndroidRuntime( 4526):在 org.apache.http.impl.client.AbstractHttpClient。执行(AbstractHttpClient.java:555)05-01 13:47:04.752:E/AndroidRuntime(4526):在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)05-01 13: 47:04.752: E/AndroidRuntime(4526): 在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 05-01 13:47:04.752: E/AndroidRuntime(4526): 在 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 46msAbstractHttpClient.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): 在 com.jacobschellenbergflickrsearch.DataDownloader.buildUrl(DataDownloader.java:31) 05-01 13:47:04.752: E/AndroidRuntime(4526): 在 com.jacobschellenbergflickrsearch.MainActivity .searchImage(MainActivity.java:47) 05-01 13:47:04.752: E/AndroidRuntime(4526): ... 14 更多 05-01 13:47:04.802: D/dalvikvm(4526): GC_CONCURRENT 释放 210K, 5%免费7474K/7812K,暂停5ms+2ms,共46msAbstractHttpClient.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): 在 com.jacobschellenbergflickrsearch.DataDownloader.buildUrl(DataDownloader.java:31) 05-01 13:47:04.752: E/AndroidRuntime(4526): 在 com.jacobschellenbergflickrsearch.MainActivity .searchImage(MainActivity.java:47) 05-01 13:47:04.752: E/AndroidRuntime(4526): ... 14 更多 05-01 13:47:04.802: D/dalvikvm(4526): GC_CONCURRENT 释放 210K, 5%免费7474K/7812K,暂停5ms+2ms,共46msE/AndroidRuntime(4526): 在 com.jacobschellenbergflickrsearch.DataDownloader.buildUrl(DataDownloader.java:31) 05-01 13:47:04.752: E/AndroidRuntime(4526): 在 com.jacobschellenbergflickrsearch.MainActivity.searchImage(MainActivity.java :47) 05-01 13:47:04.752: E/AndroidRuntime(4526): ... 14 更多 05-01 13:47:04.802: D/dalvikvm(4526): GC_CONCURRENT 释放 210K,5% 释放 7474K/7812K ,暂停5ms+2ms,共46msE/AndroidRuntime(4526): 在 com.jacobschellenbergflickrsearch.DataDownloader.buildUrl(DataDownloader.java:31) 05-01 13:47:04.752: E/AndroidRuntime(4526): 在 com.jacobschellenbergflickrsearch.MainActivity.searchImage(MainActivity.java :47) 05-01 13:47:04.752: E/AndroidRuntime(4526): ... 14 更多 05-01 13:47:04.802: D/dalvikvm(4526): GC_CONCURRENT 释放 210K,5% 释放 7474K/7812K ,暂停5ms+2ms,共46ms