1

I want my application to send two strings through the query string to a php file that will handle them as POST variables.

So far I have this code

public void postData() {    
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("www.mywebsite.com/my_phpfile.php?var1=20&var2=31");

    try {

    HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
} 

I think it's an easy problem to solve but it's my first android app and I'd appreciate all the help.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mike
  • 23
  • 1
  • 6

1 Answers1

2

Use nameValuePairs to pass data in the POST request.

Try it like this :

public void postData() {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/yourscript.php");

    try {

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "123"));
        nameValuePairs.add(new BasicNameValuePair("string", "Hey"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // Catch Protocol Exception
    } catch (IOException e) {
        // Catch IOException
    }
} 
Abhishek Sabbarwal
  • 3,758
  • 1
  • 26
  • 41