Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 3.56 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Get a remote picture cause NetworkOnMainThreadException in android
  2. public class SocialApp2Activity extends Activity {
  3.     private Handler handler;
  4.  
  5.     private Facebook facebook;
  6.     private AsyncFacebookRunner asyncRunner;
  7.  
  8.     private TextView text;
  9.     private ImageView imgUserPic;
  10.  
  11.     @Override
  12.     public void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.main);
  15.  
  16.         handler = new Handler();
  17.  
  18.         text = (TextView) findViewById(R.id.txt);
  19.         imgUserPic = (ImageView) findViewById(R.id.user_pic);
  20.  
  21.         facebook = new Facebook("MY_FB_APP_ID");
  22.         asyncRunner = new AsyncFacebookRunner(facebook);
  23.  
  24.         facebook.authorize(this, new DialogListener() {
  25.             @Override
  26.             public void onComplete(Bundle values) {
  27.                 Bundle params = new Bundle();
  28.                 params.putString("fields", "name, picture");
  29.                 asyncRunner.request("me", params, new UserRequestListener());
  30.             }
  31.  
  32.             ...
  33.  
  34.         });
  35.     }
  36.  
  37.     @Override
  38.     public void onActivityResult(int requestCode, int resultCode, Intent data) {
  39.         super.onActivityResult(requestCode, resultCode, data);
  40.  
  41.         facebook.authorizeCallback(requestCode, resultCode, data);
  42.     }
  43.  
  44.  
  45.     /* Callback for fetching username and profile picture */
  46.     public class UserRequestListener implements RequestListener {
  47.  
  48.         @Override
  49.         public void onComplete(final String response, final Object state) {
  50.  
  51.             JSONObject jsonObject;
  52.  
  53.             try {
  54.                 jsonObject = new JSONObject(response);
  55.  
  56.                 final String picURL = jsonObject.getString("picture");
  57.                 final String name = jsonObject.getString("name");
  58.  
  59.                 handler.post(new Runnable() {
  60.                     @Override
  61.                     public void run() {
  62.                         text.setText(name);
  63.  
  64.                         /********************************
  65.                          *** This line cause an error ***
  66.                          ********************************/
  67.                         imgUserPic.setImageBitmap(Utility.getBitmap(picURL));
  68.  
  69.                     }
  70.                 });
  71.             } catch (JSONException e) {
  72.                 // TODO Auto-generated catch block
  73.                 e.printStackTrace();
  74.             }
  75.         }
  76.         ...
  77.     }
  78. }
  79.        
  80. 05-04 14:37:28.898: W/System.err(1117): android.os.NetworkOnMainThreadException
  81. 05-04 14:37:28.905: W/System.err(1117):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
  82. 05-04 14:37:28.905: W/System.err(1117):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
  83. ...
  84. 05-04 14:37:29.005: W/System.err(1117):     at libcore.net.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:164)
  85. 05-04 14:37:29.005: W/System.err(1117):     at promlert.socialapp2.Utility.getBitmap(Utility.java:26)
  86. 05-04 14:37:29.016: W/System.err(1117):     at promlert.socialapp2.SocialApp2Activity$UserRequestListener$1.run(SocialApp2Activity.java:95)
  87. ...
  88.        
  89. new Thread(new Runnable() {
  90.  
  91.                 @Override
  92.                 public void run() {
  93.                     //this line must be called not from the UI  thread
  94.                 final Bitmap bitmap = Utility.getBitmap(picURL)
  95.                 handler.post(new Runnable() {
  96.                 @Override
  97.                 public void run() {
  98.                                    text.setText(name);
  99.                                    imgUserPic.setImageBitmap(bitmap );
  100.  
  101.                                   }
  102.                   });  
  103.                 }
  104. }).start();