Al Phaba
Al Phaba

Reputation: 6765

NetworkOnMainThreadException occurs although my network service is declared in its own thread?

My service uploads some files in the background and its declared like this in my manifest

<service
        android:name="uploader.services.AttachService"
        android:icon="@drawable/loading_icon"
        android:label="@string/attachServiceName"
        android:process=":attachServiceBackground" />

On Android 4.1.1 it occurs a NetworkOnMainThreadException but I do not know why. I know that since honeycomb it is not allowed to do networking on the main thread, thats why the service will run in its own thread.

Actually I am starting the service my activity like this

            startService(new Intent(MainActivity.this, AttachService.class));

Is it necessary to start the service in a AsyncTask although its declared to run in its own thread? Here is a method of my service which does not work

public static String attach(File attRequestFile, File metaDataFile, Job j) {

    String retVal = "";
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(j.getTarget().getServiceEndpoint() + ATTACH_PATH);

    MultipartEntity mp = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    mp.addPart("metadata", new FileBody(metaDataFile));
    mp.addPart("request", new FileBody(attRequestFile));

    File img = new File(j.getAtach().getAttUri());

    if (img != null){
        mp.addPart("data", new FileBody(img));      
    }


    post.setEntity(mp);


    HttpResponse response;
    try {
        response = client.execute(post);
        if (response.getEntity() == null){

            retVal = "";                
        }
        else{
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer();
            String line = "";
            while ((line = rd.readLine()) != null) {
                sb.append(line);
            }               
            retVal = sb.toString();             
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return retVal;


}

With a button in the settings view the user can start the service, like this

public void startSendDataAction(View view) { startService( new Intent( this, AttachService.class ) ); }

Any advice what the reason can be?

Thanks

Upvotes: 1

Views: 249

Answers (2)

GrIsHu
GrIsHu

Reputation: 23638

You can disable the strict mode of Thread execution using following code:

if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = 
        new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}

Again that is not recommended. Use need to use AsyncTask interface for better result.

Upvotes: 1

sdabet
sdabet

Reputation: 18670

It is sais in the documentation that

services, like other application objects, run in the main thread of their hosting process

So I guess that if you want to do some network stuff in your service you'd better do it in a background thread (whatever process it's running in).

Upvotes: 0

Related Questions