0

I have a problem with my android app. I mean I'm using the code from one of tuts in the internet, looks like below

public static String sendLocation(String s) {
    // Create a new HttpClient and Post Header
    //HttpParams params = new BasicHttpParams();
    //params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://google.com/read.php");
    //String d = "ok";

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("user", "mobile"));
        nameValuePairs.add(new BasicNameValuePair("location", s));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
      //  d = "CPE";
    } catch (IOException e) {
       // d = "E";
    }
    return "send";
} 

}`

And the problem/bug appeared when the app try to send data to the remote server (the line below)

// Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost);

I also use the StrictMode and after that everything works, the logs shows network violation

11-08 22:34:40.020: D/StrictMode(939): StrictMode policy violation; ~duration=359 ms: android.os.StrictMode$StrictModeNetworkViolation: policy=31 violation=4

I included in the manifest file appropriate permission

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

Do you have any idea what should I do to run my app (actually send data via post to the server) without StrictMode?

walmen
  • 1
  • 1

1 Answers1

1

The problem is that you are trying to do network calls on the main UI thread. Only use those types of methods in a separate thread.

A good example of that is here.

Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
HandlerExploit
  • 8,131
  • 4
  • 31
  • 50
  • what does "policy=31 violation=4" mean ? is somewhere table with list of policy and violations ? what does 31 and 4 mean ?, thanks – Lukap Jul 03 '12 at 08:44
  • See [this question](http://stackoverflow.com/a/10955043/2050) on how to find about the policy and the violation. – Eric Platon Jun 20 '14 at 08:17