1

我想从远程数据库中获取一些数据,并且我的活动的 AsyncTask 子类中有一些网络,但是当我尝试加载我的程序时,它显示 NetworkOnMainThreadException。

这是我的代码:

public class MyMapLocationActivity extends MapActivity {

private MapView mapView;
private static String url_product_details = "http://myurl.com/readfromdb";
private static final String TAG_SUCCESS = "success";
private static final String TAG_LATITUDE = "latitude";
private static final String TAG_LONGITUDE = "longitude";
private static final String TAG_POINT = "point";

    JSONParser jsonParser = new JSONParser();

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main); 

    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);

    ReadLocations Read = new ReadLocations();
    Read.execute();
}

//And my networking AsyncTask:

class ReadLocations extends AsyncTask<String, String, String> {


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }


   @Override
    protected String doInBackground(String... args) {
       Log.d("debug", "START DOINBACKGROUND");


       runOnUiThread(new Runnable() {
           public void run() {
               // Check for success tag
               int success;
               try {

                   // Building Parameters
                   List<NameValuePair> params = new ArrayList<NameValuePair>();
                   params.add(new BasicNameValuePair("id", "16"));

                   // getting product details by making HTTP request
                   // Note that product details url will use GET request
                   Log.d("debug", "before http request");

                   JSONObject json = jsonParser.makeHttpRequest(
                           url_product_details, "POST", params);
                   Log.d("debug", "after http request");

                   // json success tag
                   success = json.getInt(TAG_SUCCESS);

                   if (success == 1) {
                       // successfully received product details
                       JSONArray productObj = json
                               .getJSONArray(TAG_POINT); // JSON Array

                       // get first product object from JSON Array
                       JSONObject product = productObj.getJSONObject(0);

                   }else{
                       // product with pid not found
                   }
               } catch (JSONException e) {
                   e.printStackTrace();
               }
           }
       });

        return null; 
    }



  @Override
    protected void onPostExecute(String text) {
        // dismiss the dialog once done
   Log.d("debug", "ONPOSTEXECUTE");

    }

LogCat 显示“http 请求之前”,但不显示我的“http 请求之后”检查。

怎么了?

4

3 回答 3

5

您的网络代码在应用程序的MainUiThread上,而不是在工作线程中......

消除

runOnUiThread(new Runnable() {
           public void run() {

doInBackground()

并将WebRequest代码仅放在doInBackground()AsyncTask..

更新:

实际上,您的AsyncTask实现doInBackground()是错误的。作为 doInBackground()在工作线程中运行的冗长和网络相关操作。而且您正在尝试使用不符合规则的MainUiThread形式运行它runOnUiThread()。如果您想让任何与 UI 相关的工作在onPostExecute()ASyncTaskonPreExecute()和其他方法中进行。

于 2012-09-19T12:42:40.773 回答
1

试试这个代码:

@Override
protected String doInBackground(String... args) {
   Log.d("debug", "START DOINBACKGROUND");

           // Check for success tag
           int success;
           try {

               // Building Parameters
               List<NameValuePair> params = new ArrayList<NameValuePair>();
               params.add(new BasicNameValuePair("id", "16"));

               // getting product details by making HTTP request
               // Note that product details url will use GET request
               Log.d("debug", "before http request");

               JSONObject json = jsonParser.makeHttpRequest(
                       url_product_details, "POST", params);
               Log.d("debug", "after http request");

               // json success tag
               success = json.getInt(TAG_SUCCESS);

               if (success == 1) {
                   // successfully received product details
                   JSONArray productObj = json
                           .getJSONArray(TAG_POINT); // JSON Array

                   // get first product object from JSON Array
                   JSONObject product = productObj.getJSONObject(0);

               }else{
                   // product with pid not found
               }
           } catch (JSONException e) {
               e.printStackTrace();
           }

           return null; 
}

您的代码的问题是您仅在主 UI 线程上运行网络功能,使用 this : runOnUiThread(new Runnable() {...}

doInBackground() 自动在单独的后台非 ui 线程上运行,您无需显式创建线程。

于 2012-09-19T12:45:15.477 回答
0

那是因为您在 UI 线程上运行网络代码......您在 UI 线程doInBackground上显式运行网络代码。

于 2012-09-19T12:43:03.063 回答