I am following this website http://givemepass.blogspot.hk/2011/12/http-server.html to try to use the android application connect the PHP server to get message.
GetS
You are running network related operation on the ui thread. You will get NetworkOnMainThreadException
post honeycomb.
Use a Thread
or Asynctask
Invoke asynctask
new TheTask().execute("http://192.168.1.88/androidtesting.php");
AsyncTask
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
You need to run long running tasks, like network, calls on a separate Thread
as Sotirios Delimanolis said. AsyncTask is probably the way to go.