2013-05-01 54 views
0

我有2個設備。 Droid x3 w /薑餅和Nexus 7 w/Jellybean 4.2.2。 我在我的Droid x3上編譯並運行這段代碼,它運行完美。 然而,我在Nexus 7上運行的代碼完全相同,並且中斷了。Android代碼在薑餅上工作正常,但在Nexus 7 w/4.2.2上破解

包括代碼和堆棧跟蹤。

感謝您的幫助!

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):線程ID = 1:螺紋退出 未捕獲的異常(組= 0x41377930)05-01 13:47:04.752: E/AndroidRuntime(4526):致命異常:main 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/Andr android.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):在 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。 E/AndroidRuntime(4526):引起影響: java.lang.reflect.InvocationTargetException 05-01 13:47:04.752: E/AndroidRuntime(4526):主要原始方法05-01 13:47:04.752: 在 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.j ava: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):在 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/And roidRuntime(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

回答

6

所致:android.os .NetworkOnMainThreadException

您正在主應用程序線程上執行網絡I/O。這對任何Android版本來說都是一個非常糟糕的主意,並且它在Android 4.0+上引發了一個實際例外。

請在後臺線程上執行您的網絡I/O,例如使用AsyncTask

0

HttpResponse response = client.execute(request);

這就是你的問題。不要在UI線程上調用網絡操作。你不應該那樣做。由於ICS是禁止的。

使用AsyncTask並在doInBackground()內調用client.execute()。這將解決您的問題。

0

由於錯誤提示您正在主線程上進行新的網絡調用,您不能這樣做,它必須位於單獨的線程中

相關問題