-3

我正在尝试使用 PHP 从 APACHE 服务器上的在线数据库中获取值。这些值通过 JSON 对象返回。我在AsyncTask子类的doInBackground函数中发出 GET 请求。我正在使用 runOnUiThread 发出请求。当我在API 级别 10的模拟器上运行该应用程序时,它运行良好,但是在API 级别 17的模拟器上运行它时崩溃。

我的清单文件是

  <?xml version="1.0" encoding="utf-8"?>

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="yanat.id_card"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="10" />


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="yanat.id_card.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="yanat.id_card.DisplayDetailsActivity"
        android:label="@string/title_activity_display_details" >
    </activity>
</application>

  <uses-permission android:name="android.permission.INTERNET"/>
    </manifest>

用于解析 JSON 数据的 JSONParser 类是

            package yanat.id_card;

        import java.io.BufferedReader;
        import java.io.IOException;
        import java.io.InputStream;
        import java.io.InputStreamReader;
        import java.io.UnsupportedEncodingException;
        import java.util.List;

        import org.apache.http.HttpEntity;
        import org.apache.http.HttpResponse;
        import org.apache.http.NameValuePair;
        import org.apache.http.client.ClientProtocolException;
        import org.apache.http.client.entity.UrlEncodedFormEntity;
        import org.apache.http.client.methods.HttpGet;
        import org.apache.http.client.methods.HttpPost;
        import org.apache.http.client.utils.URLEncodedUtils;
        import org.apache.http.impl.client.DefaultHttpClient;
        import org.json.JSONException;
        import org.json.JSONObject;

        import android.util.Log;

        public class JSONParser {

            static InputStream is = null;
            static JSONObject jObj = null;
            static String json = "";
            String url_return="lol";
            // constructor
            public JSONParser() {

            }

            // function get json from url
            // by making HTTP POST or GET method
            public JSONObject makeHttpRequest(String url, String method,
                    List<NameValuePair> params) {

                // Making HTTP request
                try {

                    // check for request method
                    if(method == "POST"){
                        // request method is POST
                        // defaultHttpClient
                        DefaultHttpClient httpClient = new DefaultHttpClient();
                        HttpPost httpPost = new HttpPost(url);
                        httpPost.setEntity(new UrlEncodedFormEntity(params));

                        HttpResponse httpResponse = httpClient.execute(httpPost);
                        HttpEntity httpEntity = httpResponse.getEntity();
                        is = httpEntity.getContent();

                    }else if(method == "GET"){
                        // request method is GET
                        DefaultHttpClient httpClient = new DefaultHttpClient();
                        String paramString = URLEncodedUtils.format(params, "utf-8");
                        url += "?" + paramString;
                        HttpGet httpGet = new HttpGet(url);
                        url_return = url;
                        HttpResponse httpResponse = httpClient.execute(httpGet);
                        HttpEntity httpEntity = httpResponse.getEntity();
                        is = httpEntity.getContent();
                    }           



                    BufferedReader reader = new BufferedReader(new InputStreamReader(
                            is, "iso-8859-1"), 8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        Log.d("load",line);
                        sb.append(line + "\n");
                    }
                    Log.d("toast",url);
                    is.close();
                    json = sb.toString();

                // try parse the string to a JSON object
                    jObj = new JSONObject(json);

               } catch (UnsupportedEncodingException e) {
                   e.printStackTrace();
               } catch (ClientProtocolException e) {
                   e.printStackTrace();
               } catch (IOException e) {
                   e.printStackTrace();
               }catch (JSONException e) {
                   Log.e("JSON Parser", "Error parsing data " + e.toString());
               } catch (Exception e) {
                   Log.e("Buffer Error", "Error converting result " + e.toString());
               }

                Log.d("tag", json);
                json = "mold"; 

                // return JSON String
                if(jObj != null){
                    Log.d("tag", "msg");
                    return jObj;
                }else
                {
                    Log.d("lol", json);
                    return null;
                }

            }
            public String url(){
                return url_return;
            }
        }

我打电话的活动是

            package yanat.id_card;

        import java.util.ArrayList;
        import java.util.List;

        import org.apache.http.NameValuePair;
        import org.apache.http.message.BasicNameValuePair;
        import org.json.JSONArray;
        import org.json.JSONException;
        import org.json.JSONObject;

        import android.app.Activity;
        import android.os.AsyncTask;
        import android.os.Bundle;
        import android.util.Log;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.Button;
        import android.widget.TextView;

        public class DisplayDetailsActivity extends Activity {

            String pid;
            String name;
            String price;
            String description;
            String get_product_details="http://api.androidhive.info/android_connect/get_product_details.php";
            String message="first m9essage" ;

            JSONParser jsonParser;
            JSONObject json;

              private static final String TAG_SUCCESS = "success";
              private static final String TAG_PRODUCT = "product";
              private static final String TAG_PID = "pid";
              private static final String TAG_NAME = "name";
              private static final String TAG_PRICE = "price";
              private static final String TAG_DESCRIPTION = "description";

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.display_details);

                //get the intent from MainActivity
                Bundle extra = getIntent().getExtras();
                if(extra == null){
                    return ;
                }

                //get the strings from MainActivity
                pid = extra.getString("name");

                new GetDetails().execute();

                Button b = (Button) findViewById(R.id.button1);

                b.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
            }

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

                @Override
                protected String doInBackground(String... params) {
                    runOnUiThread(new Runnable() {

                        public void run() {
                            int success;
                            try{

                                Log.d("hocus","pocus");

                                jsonParser = new JSONParser();

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

                                // getting product details by making HTTP request
                                // Note that product details url will use GET request
                                     json = jsonParser.makeHttpRequest(
                                        get_product_details, "GET", params);


                               message = jsonParser.url();
                               Log.d("plod",message);

                               // check your log for json response
                             //  Log.d("Single Product Details",json.toString());

                               // json success tag
                               success = json.getInt(TAG_SUCCESS);
                               message="muh";
                               if (success == 1) {
                                   // successfully received product details
                                   JSONArray productObj = json
                                           .getJSONArray(TAG_PRODUCT); // JSON Array

                                   message = "names of places";

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

                                   message = "names of places2";

                                   //get the details
                                   name = product.getString("name");
                                   pid = product.getString("pid");
                                   price = product.getString("price");
                                   description = product.getString("description");

                                 TextView tView5 = (TextView) findViewById(R.id.textView5);
                                 TextView tView6 = (TextView) findViewById(R.id.textView6);
                                 TextView tView7 = (TextView) findViewById(R.id.textView7);
                                 TextView tView8 = (TextView) findViewById(R.id.textView8);

                                 tView5.setText(product.getString("name"));
                                 tView6.setText(product.getString("pid"));
                                 tView7.setText(product.getString("price"));
                                 tView8.setText(product.getString("description"));
                            }else{
                                 TextView tView5 = (TextView) findViewById(R.id.textView5);
                                 tView5.setText(json.toString());
                                 }
                        }catch(JSONException e){
                            Log.d("tag", "message");
                            e.printStackTrace();
                            }catch(NullPointerException e){
                                Log.d("error", message);
                                e.printStackTrace();
                            }
                        }

                    });

                    return null;
                }

                 protected void onPostExecute(String file_url) {
                     DatabaseHelper db = new  DatabaseHelper(
                             getApplicationContext());
                    db.insert(pid, name);
                    Log.d("message", "inserted");

                 }
                }

        }

这里提到的DatabaseHelper类不是必需的。意图值来自另一个工作正常的活动。get_product_details.php 文件是以下链接中的文件

http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/

我收到以下错误:

08-05 18:25:18.856: E/Buffer Error(1431): Error converting result android.os.NetworkOnMainThreadException

给出错误的行是

   BufferedReader reader = new BufferedReader(new InputStreamReader(
                            is, "iso-8859-1"), 8);

PS:如果有人想查看完整的 Logcat,请发表评论。我认为没有必要打印整个 Logcat。

日志猫是

            08-05 18:25:02.857: W/WindowManager(291): Failure taking screenshot for (246x437) to layer 21005
        08-05 18:25:03.196: D/dalvikvm(1431): Not late-enabling CheckJNI (already on)
        08-05 18:25:03.236: I/ActivityManager(291): Start proc yanat.id_card for activity yanat.id_card/.MainActivity: pid=1431 uid=10046 gids={50046, 3003, 1028}
        08-05 18:25:03.336: D/dalvikvm(38): GC_EXPLICIT freed 38K, 7% free 2383K/2544K, paused 4ms+16ms, total 123ms
        08-05 18:25:03.437: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property
        08-05 18:25:03.587: D/dalvikvm(38): GC_EXPLICIT freed <1K, 7% free 2383K/2544K, paused 26ms+21ms, total 245ms
        08-05 18:25:03.716: D/dalvikvm(38): GC_EXPLICIT freed <1K, 7% free 2383K/2544K, paused 9ms+5ms, total 132ms
        08-05 18:25:03.786: I/dalvikvm(1431): Turning on JNI app bug workarounds for target SDK version 10...
        08-05 18:25:03.986: E/Trace(1431): error opening trace file: No such file or directory (2)
        08-05 18:25:04.526: I/Choreographer(525): Skipped 33 frames!  The application may be doing too much work on its main thread.
        08-05 18:25:05.067: V/PhoneStatusBar(594): setLightsOn(true)
        08-05 18:25:05.146: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property
        08-05 18:25:05.186: D/dalvikvm(422): GREF has increased to 201
        08-05 18:25:05.277: D/gralloc_goldfish(1431): Emulator without GPU emulation detected.
        08-05 18:25:05.356: I/ActivityManager(291): Displayed yanat.id_card/.MainActivity: +2s186ms (total +1m13s913ms)
        08-05 18:25:05.776: I/Choreographer(525): Skipped 33 frames!  The application may be doing too much work on its main thread.
        08-05 18:25:09.306: I/Choreographer(422): Skipped 44 frames!  The application may be doing too much work on its main thread.
        08-05 18:25:09.506: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property
        08-05 18:25:13.416: I/Choreographer(422): Skipped 58 frames!  The application may be doing too much work on its main thread.
        08-05 18:25:13.716: I/Choreographer(422): Skipped 40 frames!  The application may be doing too much work on its main thread.
        08-05 18:25:14.346: I/Choreographer(422): Skipped 63 frames!  The application may be doing too much work on its main thread.
        08-05 18:25:14.546: I/Choreographer(422): Skipped 47 frames!  The application may be doing too much work on its main thread.
        08-05 18:25:14.686: I/Choreographer(422): Skipped 34 frames!  The application may be doing too much work on its main thread.
        08-05 18:25:14.827: I/Choreographer(422): Skipped 34 frames!  The application may be doing too much work on its main thread.
        08-05 18:25:15.667: I/Choreographer(422): Skipped 38 frames!  The application may be doing too much work on its main thread.
        08-05 18:25:15.856: I/Choreographer(422): Skipped 33 frames!  The application may be doing too much work on its main thread.
        08-05 18:25:17.077: I/ActivityManager(291): START u0 {cmp=yanat.id_card/.DisplayDetailsActivity (has extras)} from pid 1431
        08-05 18:25:17.087: W/WindowManager(291): Failure taking screenshot for (246x437) to layer 21015
        08-05 18:25:17.396: D/dalvikvm(1431): GC_CONCURRENT freed 100K, 8% free 2694K/2916K, paused 5ms+54ms, total 257ms
        08-05 18:25:17.906: I/Choreographer(1431): Skipped 172 frames!  The application may be doing too much work on its main thread.
        08-05 18:25:18.316: V/PhoneStatusBar(594): setLightsOn(true)
        08-05 18:25:18.408: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property
        08-05 18:25:18.506: D/hocus(1431): pocus
        08-05 18:25:18.856: E/Buffer Error(1431): Error converting result android.os.NetworkOnMainThreadException
        08-05 18:25:18.867: D/lol(1431): mold
        08-05 18:25:18.867: D/plod(1431): http://api.androidhive.info/android_connect/get_product_details.php?pid=1224
        08-05 18:25:18.876: D/error(1431): http://api.androidhive.info/android_connect/get_product_details.php?pid=1224
        08-05 18:25:18.876: W/System.err(1431): java.lang.NullPointerException
        08-05 18:25:18.906: W/System.err(1431):     at yanat.id_card.DisplayDetailsActivity$GetDetails$1.run(DisplayDetailsActivity.java:98)
        08-05 18:25:18.916: W/System.err(1431):     at android.os.Handler.handleCallback(Handler.java:725)
        08-05 18:25:18.926: W/System.err(1431):     at android.os.Handler.dispatchMessage(Handler.java:92)
        08-05 18:25:18.926: W/System.err(1431):     at android.os.Looper.loop(Looper.java:137)
        08-05 18:25:18.946: W/System.err(1431):     at android.app.ActivityThread.main(ActivityThread.java:5041)
        08-05 18:25:18.956: W/System.err(1431):     at java.lang.reflect.Method.invokeNative(Native Method)
        08-05 18:25:18.956: W/System.err(1431):     at java.lang.reflect.Method.invoke(Method.java:511)
        08-05 18:25:18.976: W/System.err(1431):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        08-05 18:25:18.976: W/System.err(1431):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        08-05 18:25:18.976: W/System.err(1431):     at dalvik.system.NativeStart.main(Native Method)
        08-05 18:25:19.267: D/AndroidRuntime(1431): Shutting down VM
        08-05 18:25:19.286: W/dalvikvm(1431): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
        08-05 18:25:19.376: E/AndroidRuntime(1431): FATAL EXCEPTION: main
        08-05 18:25:19.376: E/AndroidRuntime(1431): java.lang.NullPointerException: println needs a message
        08-05 18:25:19.376: E/AndroidRuntime(1431):     at android.util.Log.println_native(Native Method)
        08-05 18:25:19.376: E/AndroidRuntime(1431):     at android.util.Log.d(Log.java:138)
        08-05 18:25:19.376: E/AndroidRuntime(1431):     at yanat.id_card.DatabaseHelper.insert(DatabaseHelper.java:54)
        08-05 18:25:19.376: E/AndroidRuntime(1431):     at yanat.id_card.DisplayDetailsActivity$GetDetails.onPostExecute(DisplayDetailsActivity.java:148)
        08-05 18:25:19.376: E/AndroidRuntime(1431):     at yanat.id_card.DisplayDetailsActivity$GetDetails.onPostExecute(DisplayDetailsActivity.java:1)
        08-05 18:25:19.376: E/AndroidRuntime(1431):     at android.os.AsyncTask.finish(AsyncTask.java:631)
        08-05 18:25:19.376: E/AndroidRuntime(1431):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
        08-05 18:25:19.376: E/AndroidRuntime(1431):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
        08-05 18:25:19.376: E/AndroidRuntime(1431):     at android.os.Handler.dispatchMessage(Handler.java:99)
        08-05 18:25:19.376: E/AndroidRuntime(1431):     at android.os.Looper.loop(Looper.java:137)
        08-05 18:25:19.376: E/AndroidRuntime(1431):     at android.app.ActivityThread.main(ActivityThread.java:5041)
        08-05 18:25:19.376: E/AndroidRuntime(1431):     at java.lang.reflect.Method.invokeNative(Native Method)
        08-05 18:25:19.376: E/AndroidRuntime(1431):     at java.lang.reflect.Method.invoke(Method.java:511)
        08-05 18:25:19.376: E/AndroidRuntime(1431):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        08-05 18:25:19.376: E/AndroidRuntime(1431):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        08-05 18:25:19.376: E/AndroidRuntime(1431):     at dalvik.system.NativeStart.main(Native Method)
        08-05 18:25:19.476: W/ActivityManager(291):   Force finishing activity yanat.id_card/.DisplayDetailsActivity
        08-05 18:25:19.486: W/WindowManager(291): Failure taking screenshot for (246x437) to layer 21020
        08-05 18:25:19.546: W/ActivityManager(291):   Force finishing activity yanat.id_card/.MainActivity
        08-05 18:25:20.089: W/ActivityManager(291): Activity pause timeout for ActivityRecord{40eb7f50 u0 yanat.id_card/.DisplayDetailsActivity}
        08-05 18:25:20.366: I/Choreographer(291): Skipped 118 frames!  The application may be doing too much work on its main thread.
        08-05 18:25:20.596: I/Choreographer(525): Skipped 65 frames!  The application may be doing too much work on its main thread.
        08-05 18:25:20.646: I/Choreographer(291): Skipped 60 frames!  The application may be doing too much work on its main thread.
        08-05 18:25:20.676: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property
        08-05 18:25:21.146: I/Choreographer(291): Skipped 50 frames!  The application may be doing too much work on its main thread.
        08-05 18:25:21.226: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property
        08-05 18:25:23.526: D/dalvikvm(525): GC_FOR_ALLOC freed 2449K, 44% free 9172K/16128K, paused 223ms, total 229ms
        08-05 18:25:24.286: D/dalvikvm(525): GC_CONCURRENT freed 1180K, 38% free 10040K/16128K, paused 18ms+359ms, total 477ms
        08-05 18:25:24.286: D/dalvikvm(525): WAIT_FOR_CONCURRENT_GC blocked 389ms
        08-05 18:25:24.316: D/dalvikvm(525): WAIT_FOR_CONCURRENT_GC blocked 427ms
        08-05 18:25:24.316: D/dalvikvm(525): WAIT_FOR_CONCURRENT_GC blocked 435ms
        08-05 18:25:24.986: D/dalvikvm(525): GC_CONCURRENT freed 1288K, 34% free 10795K/16128K, paused 20ms+10ms, total 170ms
        08-05 18:25:24.986: D/dalvikvm(525): WAIT_FOR_CONCURRENT_GC blocked 124ms
        08-05 18:25:33.250: W/ActivityManager(291): Activity destroy timeout for ActivityRecord{40ce58b8 u0 yanat.id_card/.MainActivity}
        08-05 18:25:33.326: W/ActivityManager(291): Activity destroy timeout for ActivityRecord{40eb7f50 u0 yanat.id_card/.DisplayDetailsActivity}
        08-05 18:25:44.080: D/ExchangeService(720): Received deviceId from Email app: null
        08-05 18:25:44.080: D/ExchangeService(720): !!! deviceId unknown; stopping self and retrying
        08-05 18:25:49.206: D/ExchangeService(720): !!! EAS ExchangeService, onStartCommand, startingUp = false, running = false
        08-05 18:25:49.216: W/ActivityManager(291): Unable to start service Intent { act=com.android.email.ACCOUNT_INTENT } U=0: not found
        08-05 18:25:49.226: D/ExchangeService(720): !!! Email application not found; stopping self
        08-05 18:25:49.236: W/ActivityManager(291): Unable to start service Intent { act=com.android.email.ACCOUNT_INTENT } U=0: not found
        08-05 18:25:49.256: E/ActivityThread(720): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d4c5c8 that was originally bound here
        08-05 18:25:49.256: E/ActivityThread(720): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d4c5c8 that was originally bound here
        08-05 18:25:49.256: E/ActivityThread(720):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
        08-05 18:25:49.256: E/ActivityThread(720):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
        08-05 18:25:49.256: E/ActivityThread(720):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
        08-05 18:25:49.256: E/ActivityThread(720):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
        08-05 18:25:49.256: E/ActivityThread(720):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
        08-05 18:25:49.256: E/ActivityThread(720):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
        08-05 18:25:49.256: E/ActivityThread(720):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
        08-05 18:25:49.256: E/ActivityThread(720):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
        08-05 18:25:49.256: E/ActivityThread(720):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
        08-05 18:25:49.256: E/ActivityThread(720):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
        08-05 18:25:49.256: E/ActivityThread(720):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
        08-05 18:25:49.256: E/ActivityThread(720):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
        08-05 18:25:49.256: E/ActivityThread(720):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
        08-05 18:25:49.256: E/ActivityThread(720):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        08-05 18:25:49.256: E/ActivityThread(720):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        08-05 18:25:49.256: E/ActivityThread(720):  at java.lang.Thread.run(Thread.java:856)
        08-05 18:25:49.356: E/StrictMode(720): null
        08-05 18:25:49.356: E/StrictMode(720): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d4c5c8 that was originally bound here
        08-05 18:25:49.356: E/StrictMode(720):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
        08-05 18:25:49.356: E/StrictMode(720):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
        08-05 18:25:49.356: E/StrictMode(720):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
        08-05 18:25:49.356: E/StrictMode(720):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
        08-05 18:25:49.356: E/StrictMode(720):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
        08-05 18:25:49.356: E/StrictMode(720):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
        08-05 18:25:49.356: E/StrictMode(720):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
        08-05 18:25:49.356: E/StrictMode(720):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
        08-05 18:25:49.356: E/StrictMode(720):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
        08-05 18:25:49.356: E/StrictMode(720):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
        08-05 18:25:49.356: E/StrictMode(720):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
        08-05 18:25:49.356: E/StrictMode(720):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
        08-05 18:25:49.356: E/StrictMode(720):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
        08-05 18:25:49.356: E/StrictMode(720):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        08-05 18:25:49.356: E/StrictMode(720):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        08-05 18:25:49.356: E/StrictMode(720):  at java.lang.Thread.run(Thread.java:856)
        08-05 18:25:49.366: W/ActivityManager(291): Unbind failed: could not find connection for android.os.BinderProxy@41673718
        08-05 18:25:49.396: E/ActivityThread(720): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d52630 that was originally bound here
        08-05 18:25:49.396: E/ActivityThread(720): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d52630 that was originally bound here
        08-05 18:25:49.396: E/ActivityThread(720):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
        08-05 18:25:49.396: E/ActivityThread(720):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
        08-05 18:25:49.396: E/ActivityThread(720):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
        08-05 18:25:49.396: E/ActivityThread(720):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
        08-05 18:25:49.396: E/ActivityThread(720):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
        08-05 18:25:49.396: E/ActivityThread(720):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
        08-05 18:25:49.396: E/ActivityThread(720):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
        08-05 18:25:49.396: E/ActivityThread(720):  at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
        08-05 18:25:49.396: E/ActivityThread(720):  at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
        08-05 18:25:49.396: E/ActivityThread(720):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
        08-05 18:25:49.396: E/ActivityThread(720):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
        08-05 18:25:49.396: E/ActivityThread(720):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
        08-05 18:25:49.396: E/ActivityThread(720):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
        08-05 18:25:49.396: E/ActivityThread(720):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
        08-05 18:25:49.396: E/ActivityThread(720):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        08-05 18:25:49.396: E/ActivityThread(720):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        08-05 18:25:49.396: E/ActivityThread(720):  at java.lang.Thread.run(Thread.java:856)
        08-05 18:25:49.436: E/StrictMode(720): null
        08-05 18:25:49.436: E/StrictMode(720): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d52630 that was originally bound here
        08-05 18:25:49.436: E/StrictMode(720):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
        08-05 18:25:49.436: E/StrictMode(720):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
        08-05 18:25:49.436: E/StrictMode(720):  at 
4

4 回答 4

1

您已经在使用 AsyncTask ,它在单独的线程上执行任务。当您使用 runOnUiThread() 时,它会强制在 UI 线程上执行网络操作,这应该避免。从您的代码中删除以下内容。

runOnUiThread(new Runnable() {
    public void run() {
       //Your Code
    }
}

并按如下方式使用它:

@Override
protected String doInBackground(String... params) {
    //Your Code
    return null;
}
于 2013-08-05T18:59:30.700 回答
1

exception NetworkOnMainThreadException:

The exception that is thrown when an application attempts to perform a networking operation on its main thread.

This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged. See the document Designing for Responsiveness.

It works on API 10 because it's only thrown on 11 or higher

Why are you making the requests in runInUIThread? That's exactly what you can't do, make the request in the UI Thread. Just do it in background as asynktask is supose to.

于 2013-08-05T18:54:37.080 回答
0

在 API 级别 11 中添加了NetworkOnMainThreadException 。

当应用程序尝试在其主线程上执行网络操作时,将引发异常。

所以,只需在一个单独的线程中建立您的网络,您就可以开始了。

于 2013-08-05T18:47:23.083 回答
0

也许

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

API 17 ;)

于 2013-08-05T19:11:35.673 回答