-1

I am trying to send a picture to my local web server from my android phone. Once it connects to the web server I get the following: android.os.NetworkOnMainThreadException

I think that the solution to my problem is to make the method async. This is something I am not familiar with.

So my question is: How can I make the following method async?

public class Send {
    public Send(){
    }

public static String send(String path) throws Exception {
    String filePath = path;
    String svar;

    HttpClient httpclient = new DefaultHttpClient();
    try {
        HttpPost httppost = new HttpPost("path to web server"); 
        FileBody pic = new FileBody(new File(filePath)); 
        MultipartEntity requestEntity = new MultipartEntity(); 
        requestEntity.addPart("file", pic);

        httppost.setEntity(requestEntity);
        System.out.println("executing request " + httppost.getRequestLine());
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity responseEntity = response.getEntity();
        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());

        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
        response.getEntity().writeTo(outstream);
        byte [] responseBody = outstream.toByteArray();
        svar = new String(responseBody);
        System.out.println(svar);

    } finally {
        try {
            httpclient.getConnectionManager().shutdown();
        } 
        catch (Exception ignore) {
      }
    }
    return svar;
  }

}
Mat
  • 181
  • 1
  • 4
  • 13
  • 2
    Just tap `android.os.NetworkOnMainThreadException` on Google and you will have a lot of examples using an `AsyncTask` – Alexis C. May 21 '13 at 15:10

3 Answers3

2

Blocking operations cannot be performed on the main thread, as this would give poor user experience.

An AsyncTask is not hard to work out. The doInBackground method runs on another thread. The onPostExecute method allows you to update the UI, as this method runs on the main thread. Updating the UI is not allowed from any other threads.

  • Do I have to wrtie the AsyncTask in an activity or is it fine to put it in a regular class? I get a nullPointerException but I don't see why – Mat May 21 '13 at 20:13
  • Turns out you probably have to, It worked when I moved the code to an activity, – Mat May 21 '13 at 21:35
  • An `AsyncTask` can be a regular class: `public class MyAsyncTask extends AsyncTask`. See [Android API](http://developer.android.com/reference/android/os/AsyncTask.html) – Rogier van het Schip May 23 '13 at 09:41
0

you can not perform any network operation in main Thread. To get rid of problem do the network related tasks in another thread, or AsyncTask

For Details check this link

stinepike
  • 54,068
  • 14
  • 92
  • 112
0

Android system do not allow long operations on the UI Thread so it is free to handle all the interface operations, that include network operations or long data access.

Like others said, the correct method to do those network operations is using an AsyncTask.

This tutorial was a great help for me when i had the same problem =)

http://www.chupamobile.com/tutorial/details/116/Android+Threads,+Handlers+and+AsyncTask/

FOliveira
  • 558
  • 4
  • 9