2014-11-24 57 views
0

在AsyncTask中,我得到了一些json的一些值作爲圖像。下面是我的JSON如何從AsyncTask中的html加載圖像

{ 
    "text": "some text", 
    "html1": "Hi, check out my fb profile picture at <img src = 'http:\/\/abc.com/image1.png'\/> ", 
    "html2": "Hi, check out my whatsapp profile picture at <img src = 'http:\/\/abc.com\/image1.png'\/> ", 
    . 
    . 
    . 
} 

的樣本中異步任務postExecute方法我有

((TextView) findViewById(R.id.html1)).setText(Html.fromHtml((String) jsonObj.get("html1"), new ImageGetter() 
{ 
    @Override 
    public Drawable getDrawable(String source) 
    { 
     return loadImage(source); 
    } 
}, null)); 
((TextView) findViewById(R.id.html2)).setText(Html.fromHtml((String) jsonObj.get("html2"), new ImageGetter() 
    { 
     @Override 
     public Drawable getDrawable(String source) 
     { 
      return loadImage(source); 
     } 
    }, null)); 

和我的LoadImage方法看起來像下面

private Drawable loadImage(String source) 
    { 
     Drawable drawable = null; 
     URL sourceURL; 
     try 
     { 
      sourceURL = new URL(source); 
      URLConnection urlConnection = sourceURL.openConnection(); 
      urlConnection.connect(); 
      InputStream inputStream = urlConnection.getInputStream(); 
      BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); 
      Bitmap bm = BitmapFactory.decodeStream(bufferedInputStream); 

      // convert Bitmap to Drawable 
      drawable = new BitmapDrawable(getResources(), bm); 

      drawable.setBounds(0, 0, bm.getWidth(), bm.getHeight()); 

     } 
     catch (MalformedURLException e) 
     { 
      e.printStackTrace(); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
     return drawable; 
    } 

我覺得代碼就足夠了,但我我越來越android.os.NetworkOnMainThreadException

我在manifest.xml中添加Internet權限

+0

可以請你調試的具體行代碼時會崩潰嗎? – ymz 2014-11-24 00:31:37

+0

@ urlConnection.connect(); – 2014-11-24 00:35:44

+0

我不確定,但可能你不應該在onPostExecute方法中使用網絡。 AsyncTask中您可以執行此操作的唯一方法是doInBackground方法。這可能會導致android.os.NetworkOnMainThreadException。 – antonpp 2014-11-24 00:48:59

回答

0

當應用程序嘗試在其主線程上執行聯網操作時,會引發此異常。在AsyncTask

public class MainActivity extends Activity implements ImageGetter { 
 
    private final static String TAG = "MainActivity"; 
 
    private TextView mTv; 
 

 
    @Override 
 
    public void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_main); 
 
     String source = <your source from json here>; 
 

 
     Spanned spanned = Html.fromHtml(source, this, null); 
 
     mTv = (TextView) findViewById(R.id.text); 
 
     mTv.setText(spanned); 
 
    } 
 

 
    @Override 
 
    public Drawable getDrawable(String source) { 
 
     LevelListDrawable d = new LevelListDrawable(); 
 
     Drawable empty = getResources().getDrawable(R.drawable.ic_launcher); 
 
     d.addLevel(0, 0, empty); 
 
     d.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight()); 
 

 
     new LoadImage().execute(source, d); 
 

 
     return d; 
 
    } 
 

 
    class LoadImage extends AsyncTask<Object, Void, Bitmap> { 
 

 
     private LevelListDrawable mDrawable; 
 

 
     @Override 
 
     protected Bitmap doInBackground(Object... params) { 
 
      String source = (String) params[0]; 
 
      mDrawable = (LevelListDrawable) params[1]; 
 
      Log.d(TAG, "doInBackground " + source); 
 
      try { 
 
       InputStream is = new URL(source).openStream(); 
 
       return BitmapFactory.decodeStream(is); 
 
      } catch (FileNotFoundException e) { 
 
       e.printStackTrace(); 
 
      } catch (MalformedURLException e) { 
 
       e.printStackTrace(); 
 
      } catch (IOException e) { 
 
       e.printStackTrace(); 
 
      } 
 
      return null; 
 
     } 
 

 
     @Override 
 
     protected void onPostExecute(Bitmap bitmap) { 
 
      Log.d(TAG, "onPostExecute drawable " + mDrawable); 
 
      Log.d(TAG, "onPostExecute bitmap " + bitmap); 
 
      if (bitmap != null) { 
 
       BitmapDrawable d = new BitmapDrawable(bitmap); 
 
       mDrawable.addLevel(1, 1, d); 
 
       mDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
 
       mDrawable.setLevel(1); 
 
       // i don't know yet a better way to refresh TextView 
 
       // mTv.invalidate() doesn't work as expected 
 
       CharSequence t = mTv.getText(); 
 
       mTv.setText(t); 
 
      } 
 
     } 
 
    } 
 
}

或運行代碼: 在OnCreate中添加StrictMode代碼下的setContentView(...);

StrictMode.ThreadPolicy policy = new 
 
StrictMode.ThreadPolicy.Builder() 
 
.permitAll().build(); 
 
StrictMode.setThreadPolicy(policy);

但添加StrictMode.ThreadPolicy是一種不好的做法

0

看起來,對於URL JSON格式導致所有麻煩

沒有在這個問題上一個偉大的職位:Decode Json urls

但結束了死鏈接..所以..谷歌搜索周圍的一點點:https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html

你應該取消轉義與unescapeJavaScript功能的URL字符串以獲得正確的URL

+0

對不起,這不是問題。我試過已經 – 2014-11-24 00:49:53

+0

有機會分享內部異常信息? – ymz 2014-11-24 01:00:10