java - Comment puis-je réparer 39;android.os. NetworkOnMainThreadException

Mots clés : javaandroidandroid-networkingnetworkonmainthreadjava

meilleur 4 Réponses java - Comment puis-je réparer 39;android.os. NetworkOnMainThreadException

vote vote

92

class RetrieveFeedTask extends AsyncTask<String, Void, RSSFeed> {      private Exception exception;      protected RSSFeed doInBackground(String... urls) {         try {             URL url = new URL(urls[0]);             SAXParserFactory factory = SAXParserFactory.newInstance();             SAXParser parser = factory.newSAXParser();             XMLReader xmlreader = parser.getXMLReader();             RssHandler theRSSHandler = new RssHandler();             xmlreader.setContentHandler(theRSSHandler);             InputSource is = new InputSource(url.openStream());             xmlreader.parse(is);              return theRSSHandler.getFeed();         } catch (Exception e) {             this.exception = e;              return null;         } finally {             is.close();         }     }      protected void onPostExecute(RSSFeed feed) {         // TODO: check this.exception         // TODO: do something with the feed     } } 
new RetrieveFeedTask().execute(urlToRssFeed); 
<uses-permission android:name="android.permission.INTERNET"/> 
vote vote

89

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();  StrictMode.setThreadPolicy(policy); 
<uses-permission android:name="android.permission.INTERNET"/> 
vote vote

70

Thread thread = new Thread(new Runnable() {      @Override     public void run() {         try  {             //Your code goes here         } catch (Exception e) {             e.printStackTrace();         }     } });  thread.start();  
vote vote

70

import android.app.IntentService; import android.app.PendingIntent; import android.content.Intent; import android.util.Log;  import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL;  public class DownloadIntentService extends IntentService {      private static final String TAG = DownloadIntentService.class.getSimpleName();      public static final String PENDING_RESULT_EXTRA = "pending_result";     public static final String URL_EXTRA = "url";     public static final String RSS_RESULT_EXTRA = "url";      public static final int RESULT_CODE = 0;     public static final int INVALID_URL_CODE = 1;     public static final int ERROR_CODE = 2;      private IllustrativeRSSParser parser;      public DownloadIntentService() {         super(TAG);          // make one and reuse, in the case where more than one intent is queued         parser = new IllustrativeRSSParser();     }      @Override     protected void onHandleIntent(Intent intent) {         PendingIntent reply = intent.getParcelableExtra(PENDING_RESULT_EXTRA);         InputStream in = null;         try {             try {                 URL url = new URL(intent.getStringExtra(URL_EXTRA));                 IllustrativeRSS rss = parser.parse(in = url.openStream());                  Intent result = new Intent();                 result.putExtra(RSS_RESULT_EXTRA, rss);                  reply.send(this, RESULT_CODE, result);             } catch (MalformedURLException exc) {                 reply.send(INVALID_URL_CODE);             } catch (Exception exc) {                 // could do better by treating the different sax/xml exceptions individually                 reply.send(ERROR_CODE);             }         } catch (PendingIntent.CanceledException exc) {             Log.i(TAG, "reply cancelled", exc);         }     } } 
<service         android:name=".DownloadIntentService"         android:exported="false"/> 
PendingIntent pendingResult = createPendingResult(     RSS_DOWNLOAD_REQUEST_CODE, new Intent(), 0); Intent intent = new Intent(getApplicationContext(), DownloadIntentService.class); intent.putExtra(DownloadIntentService.URL_EXTRA, URL); intent.putExtra(DownloadIntentService.PENDING_RESULT_EXTRA, pendingResult); startService(intent); 
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {     if (requestCode == RSS_DOWNLOAD_REQUEST_CODE) {         switch (resultCode) {             case DownloadIntentService.INVALID_URL_CODE:                 handleInvalidURL();                 break;             case DownloadIntentService.ERROR_CODE:                 handleError(data);                 break;             case DownloadIntentService.RESULT_CODE:                 handleRSS(data);                 break;         }         handleRSS(data);     }     super.onActivityResult(requestCode, resultCode, data); } 

Questions similaires