Java – HttpClient.execute (HttpPost) on Android 4.2 error

HttpClient.execute (HttpPost) on Android 4.2 error… here is a solution to the problem.

HttpClient.execute (HttpPost) on Android 4.2 error

I’m following the site http://givemepass.blogspot.hk/2011/12/http-server.html try to connect to a PHP server using an android app to get messages.

GetServerMessage.java

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class GetServerMessage {

public String stringQuery(String url){
        try
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost method = new HttpPost(url);
            HttpResponse response = httpclient.execute(method);
            HttpEntity entity = response.getEntity();
            if(entity != null){
                return EntityUtils.toString(entity);
            }
            else{
                return "No string.";
            }
         }
         catch(Exception e){
             return "Network problem";
         }
    }
}

GetPhpServerMessageDemoActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class GetPhpServerMessageDemoActivity extends Activity {
    /** Called when the activity is first created. */
    private TextView textView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView = (TextView)findViewById(R.id.text_view);
        GetServerMessage message = new GetServerMessage();
        String msg = message.stringQuery("http://192.168.1.88/androidtesting.php");
        textView.setText("Server message is "+msg);
    }

}

I’m trying to download the Android app project from this site http://uploadingit.com/file/d4632ekfpwjiupyn/GetPhpServerMessageDemo2.zip running on my phone and it works.
But when I start a new project (Minimuim Requied SDK: API8, Target SDK: API17, Compile: API17) and copy both java code. I ADDED PERMISSIONS ANDROID.PERMISSION.INTERNET, SO I DON’T KNOW WHERE THE PROBLEM IS, I JUST KNOW WHEN RUNNING HTTPRESPONSE = httpclient.execute(method); An error occurs and returns the string “Network problem”.

Solution

Update: If you are using kotlin, use coroutines for threading. AsncTask is no longer used.

You are running network-related operations on a UI thread. You get NetworkOnMainThreadException post honeycomb.

Use Thread or Asynctask

Invoke an asynchronous task

   new TheTask().execute("http://192.168.1.88/androidtesting.php");

Asynchronous tasks

http://developer.android.com/reference/android/os/AsyncTask.html

class TheTask extends AsyncTask<String,String,String>
    {

@Override
protected String onPostExecute(Void result) {
     TODO Auto-generated method stub
    super.onPostExecute(result);
     update textview here
    textView.setText("Server message is "+result);
}

@Override
protected void onPreExecute() {
     TODO Auto-generated method stub
    super.onPreExecute();
}

@Override
protected String doInBackground(String... params) {
     try
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost method = new HttpPost(params[0]);
            HttpResponse response = httpclient.execute(method);
            HttpEntity entity = response.getEntity();
            if(entity != null){
                return EntityUtils.toString(entity);
            }
            else{
                return "No string.";
            }
         }
         catch(Exception e){
             return "Network problem";
         }
   
}
}

Update HttpClient is deprecated in API 23. Use HttpUrlConnection.

http://developer.android.com/reference/java/net/HttpURLConnection.html

Related Problems and Solutions