[Android Studio] mantlabs:
Hello,

Make a copy of the send(View v) method and name it sendAsync(View v, EditText msgbox).

Like this:

public Exception sendAsync(View v, EditText msgbox)
{
[B] HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("localhost");[/B]
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "01"));
nameValuePairs.add(new BasicNameValuePair("name", msgbox.getText().toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
return null;
} catch (ClientProtocolException e) {
return e;
} catch (IOException e) {
return e;
}

}

Next, modify the original send(View v) method like this:

public void send(View v)
{
//get message from message box
String msg = msgTextField.getText().toString();

//check whether the msg empty or not
if(msg.length()>0) {
MyAsyncTask mAsync = new MyAsyncTask(v, getBaseContext(), msgTextField);
mAsync.execute();
} else {
//display message if text field is empty
Toast.makeText(getBaseContext(),"All fields are required",Toast.LENGTH_SHORT).show();
}
}

You can add the below code inside your activity class (outside the onCreate method)

private class MyAsyncTask extends AsyncTask<String, Void, String> {
View v;
Context context;
EditText msgbox;
Exception e = null;
public MyAsyncTask(View v_, Conext context_, EditText msgbox_){
this.v = v_;
this.context = context_;
this.msgbox = msgbox_;
}

@Override
protected String doInBackground(String... params) {
//Background operation in a separate thread
//Write here your code to run in the background thread
this.e = sendAsync(v, msgbox);

return null;
}

@Override
protected void onPostExecute(String result) {
//Called on Main UI Thread. Executed after the Background operation, allows you to have access to the UI
if(e==null){
//Successful

msgbox.setText(""); //reset the message text field
Toast.makeText(context,"Successfully Booked!!",Toast.LENGTH_SHORT).show();
}
else{
e.printStackTrace();
}

}

@Override
protected void onPreExecute() {
//Called on Main UI Thread. Executed before the Background operation, allows you to have access to the UI
}

@Override
protected void onProgressUpdate(Void... values) {
//Called on Main UI Thread
}
}

However, HttpClient is deprecated for API Level 22 and above, so you should use HttpURLConnection instead. This may help you out for using HttpURLConnection via POST or GET Method.

Now, the doInBackground method, runs in a background thread, automatically created for you, and so we send the data from there, for not crashing the UI (User Interface) of the Android App. If you want, however, to access some UI components, you can do it in the onPostExecute (it is executed after the doInBackground completes) or in the onPreExecute (it is executed before the doInBackground begins). To learn more about the AsyncTask class, check here.

AsyncTask can be used and for other purposes other than sending data over the Internet. In general, you create a class extending the AsyncTask, pass any parameters that you are going to use in the background thread (but not UI components like EditText, you can pass them though, but use them in the onPreExecute or onPostExecute methods, and not in the doInBackground method), and do the stuff you want to run in the background thread inside the doInBackground method. That way, heavy(time consuming) computations or tasks like sending data over the Internet does not make your UI to crash, meaning that the user can press any buttons (for example) without lag or without waiting for the background task to finish.