Reputation: 115
I want to execute the php url from android and pass the two values while executing the url.
Here is the code I use. I used GET
method but I need to send it as POST variable.
How can I change the request from GET
variable to POST
variable in the below code:
public class AsyncTaskOperation extends AsyncTask <String, Void, Void>
{
@Override
protected Void doInBackground(String... paramsObj)
{
//Sending the php file path
String php_send="http://localhost/tested/test/copy_fiel.php?Cus="+Cus+"&"+"Cut_fol="+Cut_fol;
System.out.println(php_send);
// want to execute the above path using Http client but it is not working
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(php_send);
try {
HttpResponse resp = client.execute(httpGet);
System.out.println(resp);
}
catch(Exception e) {
e.printStackTrace();
}
return null;
}
}
Upvotes: 2
Views: 1358
Reputation: 4936
I use Asynchttp client. I find it easier to set it post or get
To post data: - Create a parameters object, - put in your values and post to the url.
Code sample is as follows:
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params=new RequestParams();
params.put("msg","Hello world");
url="[Your URL]";
client.post(url,params, new AsyncHttpResponseHandler() {
@Override
public void onStart() {
super.onStart();
}
@Override
public void onSuccess(String response) {
}
@Override
public void onFailure(Throwable e, String response) {
}
);
You can get the library here
http://loopj.com/android-async-http/
Upvotes: 2
Reputation: 7439
Use NameValuePair with HttpPost, it will work:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/myexample.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("Cus", Cus));
nameValuePairs.add(new BasicNameValuePair("Cut_fol", Cut_fol));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
Upvotes: 2