0

我有 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

4

3 回答 3

5

引起:android.os.NetworkOnMainThreadException

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

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

于 2013-05-01T20:52:56.950 回答
0

HttpResponse 响应 = client.execute(request);

那是你的问题。不要在 UI 线程上调用网络操作。你永远不应该那样做。由于ICS是禁止的。

使用 AsyncTask 并在 doInBackground() 中调用 client.execute()。这将解决您的问题。

于 2013-05-01T20:53:28.660 回答
0

正如错误所说,您正在主线程上进行 newtwork 调用...您不能这样做,它必须在单独的线程中

于 2013-05-01T20:53:38.687 回答