Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sethdaniel/5367607 to your computer and use it in GitHub Desktop.
Save sethdaniel/5367607 to your computer and use it in GitHub Desktop.
Of all the different methods of getting Android to do a simple POST to server and receive the response, this is the method that makes me want to hang myself the least. It's really an inspiring achievement how Android manages to provide so many different ways to take something as simple as a POST and absolutely bury it in baffling amounts of comp…
private class PostVarToPhpAndGetResponseAsString extends
AsyncTask<Void, Void, Void> {
 
ProgressDialog progressDialog = new ProgressDialog(
ParentClassActivity.this);//<---the name of the outer class goes here!!!
 
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog
.setMessage("Whatever message you want the user to see while waiting...");
progressDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
 
responseFromPhpStr = postData(someVarToSendToPhp);
 
return null;
}
@Override
protected void onPostExecute(Void result) {
progressDialog.dismiss();
if (responseFromPhpStr.equals("")) {
Log.d("ParentClassActivity/PostVarToPhpAndGetResponseAsString/onPostExecute/no_response_from_server ",
"responseFromPhpStr = " + responseFromPhpStr);
responseFromPhpStr = "no response from server";
} else {
Log.d("ParentClassActivity/PostVarToPhpAndGetResponseAsString/onPostExecute/got_response_from_server ",
"responseFromPhpStr = " + responseFromPhpStr);
/////DO SOMETHING WITH YOUR RESPONSE STRING HERE!!!/////
}
 
}
public String postData(String valueIWantToSend) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://somewebsite.com/somefiletocatchandroidposts.php");
 
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name_of_var_to_catch_server_side", valueIWantToSend));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
 
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
//Convert http response to string
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
 
responseFromPhpStr = convertStreamToString(instream);
Log.d("ParentClassActivity/PostVarToPhpAndGetResponseAsString/postData responseFromPhpStr = ", responseFromPhpStr);
}else{
responseFromPhpStr = "";
}
 
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return responseFromPhpStr;
}
public String convertStreamToString(InputStream inputStream) throws IOException {
if (inputStream != null) {
StringBuilder sb = new StringBuilder();
String line;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
} finally {
inputStream.close();
}
return sb.toString();
} else {
return "";
}
}
 
 
 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment