Rakeeb Rajbhandari
Rakeeb Rajbhandari

Reputation: 5063

NetworkOnMainThreadException on 3.0 and higher

I did learn about this on an article that, on 3.0 and higher, one has to use the AsyncTask to preform any network activity. Knowing this, I used the AsyncTask to call on a JSON request, and my main class looks like this:

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);
    }

}

In this Activity I have a OnClickListener that calls this AsyncTask even so the program is giving me the following

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()
    {

Am I implementing this the right way ?? Please Help!

I also have implemented the Google PlacesAutocomplete in this app, in which the results/suggestions do show but are not select able on 3.0 or higher a Unexpected value from nativeGetEnabledTags: 0 error shows up. The code is as follows:

location.setOnItemClickListener(new OnItemClickListener() {

@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());
            }
});

Upvotes: 1

Views: 79

Answers (4)

Himeshgiri gosvami
Himeshgiri gosvami

Reputation: 2884

You must Set your network operation on doInBackground() Method

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

Must write in doInBackground()

Upvotes: 1

Sujith
Sujith

Reputation: 7753

Dont do network operations in pre-execute. Do network operations in doInBackground, that is the correct way.

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){
        }
}

Upvotes: 1

stinepike
stinepike

Reputation: 54692

It seems your distanceGET method is doing network operation

According to doc here

onPreExecute(), invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.

Do your network things in doInBackground

Upvotes: 1

buptcoder
buptcoder

Reputation: 2702

It seems this code are getting data from network. You have put it in workthread, put it on the doinBackground();

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

Upvotes: 3

Related Questions