3.0及更高版本的NetworkOnMainThreadException

时间:2013-05-10 03:28:55

标签: android json

我确实在一篇文章中了解到这一点,在3.0及更高版本中,必须使用AsyncTask来预先形成任何网络活动。知道了这一点,我使用AsyncTask来调用JSON请求,我的主要类看起来像这样:

MainPage.java

public class doTheFare extends AsyncTask<Void,Void,Void>
{

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        progress = new ProgressDialog(MainPage.this);
        progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progress.setMessage("Processing Request ...");
        progress.show();
        UserFunctions users = new UserFunctions();
        jObj = users.distanceGET(location.getText().toString(), destination.getText().toString());
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        try{
            jArr = jObj.getJSONArray("rows");
            jObj1 = jArr.getJSONObject(0);
            jArr2 = jObj1.getJSONArray("elements");
            jObj2 = jArr2.getJSONObject(0);
            jObj3 = jObj2.getJSONObject("distance");
            distance_value = jObj3.getString("text");
            String value = jObj3.getString("value");
            SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
            String sF = getPrefs.getString("startFare", "10");
            String pM = getPrefs.getString("perKilo", "27");
            startFare = Double.parseDouble(sF);
            perKilo = Double.parseDouble(pM);
            perMeter=perKilo/5;
            int parseDistance = Integer.parseInt(value);
            double fare = startFare+((parseDistance/200)*perMeter);
            int roundedValue = (int)Math.round(fare);
            //Fare to send
            taxiFare = String.valueOf(roundedValue);
            Log.i("Fare Estimate", taxiFare);
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;

    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        progress.dismiss();
        from_split = location.getText().toString();
        to_split = destination.getText().toString();
        if(from_split.contains(","))
        {
            from_fill = from_split.split(",");
        }
        else
        {
            from_fill = from_split.split(" ");
        }
        if(to_split.contains(","))
        {
            to_fill = to_split.split(",");
        }
        else
        {
            to_fill = to_split.split(" ");
        }

        send_location=from_fill[0];
        send_destination=to_fill[0];
        send_fare = taxiFare;
        send_distance = distance_value;
        if(isMode.equalsIgnoreCase(""))
        {
            Calendar c = Calendar.getInstance();
            int hours = c.get(Calendar.HOUR_OF_DAY);
            if(hours<6)
                isMode = "nMode";
            else if(hours>=21)
                isMode = "nMode";
            else
                isMode = "dMode";
        }
        Intent toResults = new Intent(MainPage.this, ResultsDisplay.class);
        Bundle extras = new Bundle();
        extras.putString("TO", send_location);
        extras.putString("FROM", send_destination);
        extras.putString("FARE", send_fare);
        extras.putString("DISTANCE", send_distance);
        extras.putString("GMAPFROM", from_split.toString());
        extras.putString("GMAPTO", to_split.toString());
        extras.putString("MODE",isMode);
        toResults.putExtras(extras);
        startActivity(toResults);
        super.onPostExecute(result);
    }

}

Activity OnClickListener我有一个AsyncTask来调用此05-10 03:02:19.823: E/AndroidRuntime(1815): FATAL EXCEPTION: main 05-10 03:02:19.823: E/AndroidRuntime(1815): android.os.NetworkOnMainThreadException 05-10 03:02:19.823: E/AndroidRuntime(1815): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 05-10 03:02:19.823: E/AndroidRuntime(1815): at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 05-10 03:02:19.823: E/AndroidRuntime(1815): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 05-10 03:02:19.823: E/AndroidRuntime(1815): at java.net.InetAddress.getAllByName(InetAddress.java:214) 05-10 03:02:19.823: E/AndroidRuntime(1815): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 05-10 03:02:19.823: E/AndroidRuntime(1815): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 05-10 03:02:19.823: E/AndroidRuntime(1815): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 05-10 03:02:19.823: E/AndroidRuntime(1815): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 05-10 03:02:19.823: E/AndroidRuntime(1815): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 05-10 03:02:19.823: E/AndroidRuntime(1815): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 05-10 03:02:19.823: E/AndroidRuntime(1815): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 05-10 03:02:19.823: E/AndroidRuntime(1815): at com.example.calculator_taxi_fare.functions.JSONParser.getJSONFromUrl(JSONParser.java:56) 05-10 03:02:19.823: E/AndroidRuntime(1815): at com.example.calculator_taxi_fare.functions.UserFunctions.distanceGET(UserFunctions.java:28) 05-10 03:02:19.823: E/AndroidRuntime(1815): at com.example.calculator_taxi_fare.MainPage$doTheFare.onPreExecute(MainPage.java:251) 05-10 03:02:19.823: E/AndroidRuntime(1815): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586) 05-10 03:02:19.823: E/AndroidRuntime(1815): at android.os.AsyncTask.execute(AsyncTask.java:534) 05-10 03:02:19.823: E/AndroidRuntime(1815): at com.example.calculator_taxi_fare.MainPage$6.onClick(MainPage.java:230) 05-10 03:02:19.823: E/AndroidRuntime(1815): at android.view.View.performClick(View.java:4202) 05-10 03:02:19.823: E/AndroidRuntime(1815): at android.view.View$PerformClick.run(View.java:17340) 05-10 03:02:19.823: E/AndroidRuntime(1815): at android.os.Handler.handleCallback(Handler.java:725) 05-10 03:02:19.823: E/AndroidRuntime(1815): at android.os.Handler.dispatchMessage(Handler.java:92) 05-10 03:02:19.823: E/AndroidRuntime(1815): at android.os.Looper.loop(Looper.java:137) 05-10 03:02:19.823: E/AndroidRuntime(1815): at android.app.ActivityThread.main(ActivityThread.java:5039) 05-10 03:02:19.823: E/AndroidRuntime(1815): at java.lang.reflect.Method.invokeNative(Native Method) 05-10 03:02:19.823: E/AndroidRuntime(1815): at java.lang.reflect.Method.invoke(Method.java:511) 05-10 03:02:19.823: E/AndroidRuntime(1815): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 05-10 03:02:19.823: E/AndroidRuntime(1815): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 05-10 03:02:19.823: E/AndroidRuntime(1815): at dalvik.system.NativeStart.main(Native Method) location.setOnItemClickListener(new OnItemClickListener() { ,即使这样,该程序也会给我以下内容

PlacesAutocomplete

我是以正确的方式实施的吗?请帮助!

我也在此应用中实施了Google 3.0 or higher,其中结果/建议确实显示但在Unexpected value from nativeGetEnabledTags: 0上无法选择能够显示@Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { // TODO Auto-generated method stub autoLocation = (Places) adapterView.getItemAtPosition(position); location.setText(autoLocation.description); location.clearFocus(); destination.requestFocus(); Log.d("AutoCompleteTask", location.getText().toString()); } }); destination.setAdapter(new PlacesAutoCompleteAdapter(this, R.layout.list_item)); destination.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { // TODO Auto-generated method stub autoDestination = (Places) adapterView.getItemAtPosition(position); destination.setText(autoDestination.description); destination.clearFocus(); calculate.requestFocus(); Log.d("AutoCompleteTask2", destination.getText().toString()); } }); 错误。代码如下:

location.setOnItemClickListener(new OnItemClickListener()     {

{{1}}

4 个答案:

答案 0 :(得分:3)

看来这段代码正在从网络获取数据。 你把它放在workthread中,把它放在doinBackground();

        jObj = users.distanceGET(location.getText().toString(), destination.getText().toString());

答案 1 :(得分:1)

您的distanceGET方法似乎正在进行网络操作

根据文件here

  

onPreExecute(),在执行任务之前在UI线程上调用。   此步骤通常用于设置任务,例如通过显示   用户界面中的进度条。

doInBackground

中处理您的网络内容

答案 2 :(得分:1)

您必须在doInBackground()方法

上设置网络操作
jObj = users.distanceGET(location.getText().toString(), destination.getText().toString())

必须写入doInBackground()

答案 3 :(得分:1)

不要在预执行中执行网络操作。在doInBackground中进行网络操作,这是正确的方法。

protected Void doInBackground(Void... arg0) {
    // TODO Auto-generated method stub
UserFunctions users = new UserFunctions();
    jObj = users.distanceGET(location.getText().toString(),
           destination.getText().toString());
    try{
        jArr = jObj.getJSONArray("rows");
        jObj1 = jArr.getJSONObject(0);
        --------------------
        --------------------
        }catch(Exception e){
        }
}