Android代码在Gingerbread上工作正常,但在Nexus 7 w / 4.2.2上有所突破

时间:2013-05-01 20:50:38

标签: java android json eclipse

我有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:线程退出   未捕获的异常(组= 0x41377930)05-01 13:47:04.752:   E / AndroidRuntime(4526):致命异常:主05-01 13:47:04.752:   E / AndroidRuntime(4526):java.lang.IllegalStateException:无法   执行活动的方法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):引起:   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更多05-01 13:47:04.752:   E / AndroidRuntime(4526):引起:   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更多05-01   13:47:04.802:D / dalvikvm(4526):GC_CONCURRENT释放210K,5%免费   7474K / 7812K,暂停5ms + 2ms,总计46ms

3 个答案:

答案 0 :(得分:6)

  

引起:android.os.NetworkOnMainThreadException

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

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

答案 1 :(得分:0)

HttpResponse response = client.execute(request);

这是你的问题。不要在UI线程上调用网络操作。你不应该这样做。因为ICS是被禁止的。

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

答案 2 :(得分:0)

由于错误显示您正在主线程上进行网络调用...您不能这样做,它必须位于单独的线程中