Android Http Post + Web Service Php
I have a web service in PHP which returns a String, he recives two parametres, id and te. I've tested its using with the mozzila addon poster, so i decided to use it for my android
Solution 1:
for Http Post i'll suggest you to use AsyncTask<> which will run in separate thread from UI here is the code i am using since two months and its working fine
privateclassHttpAsyncTaskextendsAsyncTask<String, Void, String> {
private String id,te;
publicHttpAsyncTask(String id,String te){
this.id = id;
this.te = te;
}
@Overrideprotected String doInBackground(String... urls) {
return POST(urls[0]);
}
// onPostExecute displays the results of the AsyncTask.@OverrideprotectedvoidonPostExecute(String result) {
Toast.makeText(getBaseContext(), "Data Sent!", Toast.LENGTH_LONG).show();
}
}
publicstatic String POST(String url){
InputStreaminputStream=null;
Stringresult="";
try {
// 1. create HttpClientHttpClienthttpclient=newDefaultHttpClient();
// 2. make POST request to the given URLHttpPosthttpPost=newHttpPost(url);
// pass parameters in this way
List<NameValuePair> nameValuePairs = newArrayList<NameValuePair>(2);
nameValuePairs.add(newBasicNameValuePair("id", "value "));
nameValuePairs.add(newBasicNameValuePair("te", "value"));
//add data
httppost.setEntity(newUrlEncodedFormEntity(nameValuePairs));
// 8. Execute POST request to the given URLHttpResponsehttpResponse= httpclient.execute(httpPost);
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// 10. convert inputstream to stringif(inputStream != null)
result = convertInputStreamToString(inputStream);
elseresult="Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
// 11. return resultreturn result;
}
privatestatic String convertInputStreamToString(InputStream inputStream)throws IOException {
// TODO Auto-generated method stubBufferedReaderbufferedReader=newBufferedReader( newInputStreamReader(inputStream));
Stringline="";
Stringresult="";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
for passing values you can create constructor like this.
form your activity do this
HttpAsyncTaskhttpAsyncTask=newHttpAsyncTask(id,te);//this will pass variables values StringResultfromServer= httpAsyncTask.execute(urlStr);// String ResultfromServer is your response string
and in AsyncTask I've created constructor
this is very helpful for me hope it will help you too
Solution 2:
Look at below code for posting data to php
classPlaceOrderextendsAsyncTask<Void, Void, Void> {
@Overrideprotected Void doInBackground(Void... params) {
// TODO Auto-generated method stubtry {
HttpClienthttpClient=newDefaultHttpClient();
HttpPosthttpPst=newHttpPost(
"http://appdomain.hol.es/webService.php");
ArrayList<NameValuePair> parameters = newArrayList<NameValuePair>(
2);
// add ur parameter here
parameters.add(newBasicNameValuePair("id", value1);
parameters.add(newBasicNameValuePair("te", value2);
httpPst.setEntity(newUrlEncodedFormEntity(parameters));
HttpResponsehttpRes= httpClient.execute(httpPst);
String str=convertStreamToString(httpRes.getEntity().getContent()).toString();
Log.i("mlog","outfromurl"+str);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
returnnull;
}
}
publicstatic String convertStreamToString(InputStream is) {
BufferedReaderreader=newBufferedReader(newInputStreamReader(is));
StringBuildersb=newStringBuilder();
Stringline=null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
Post a Comment for "Android Http Post + Web Service Php"