walmen
walmen

Reputation: 1

Android httppost network violation

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?

Upvotes: 0

Views: 2210

Answers (1)

HandlerExploit
HandlerExploit

Reputation: 8251

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.

Upvotes: 1

Related Questions