0

I have two string ,how can i pass two string from android code to php? I have the follow code to connect my php:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.101/webservice/index.php");

HttpResponse response = httpclient.execute(httppost);
Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
  • Here are some related questions http://stackoverflow.com/questions/3505930/make-an-http-request-with-android http://stackoverflow.com/questions/2938502/sending-post-data-in-android – Boban S. Jan 08 '15 at 15:15

4 Answers4

1

Did you try using GET, this way, for example?

HttpClient httpclient = new DefaultHttpClient();
String string1 = "A";
String string2 = "B";
HttpPost httppost = new HttpPost("http://192.168.1.101/webservice/index.php?string1=" + string1 + "&string2=" + string2);
HttpResponse response = httpclient.execute(httppost);

And, in PHP, for example:

<?php
    $string1 = $_GET["string1"]);
    $string2 = $_GET["string2"]);
?>
MarcoS
  • 17,323
  • 24
  • 96
  • 174
1

Do this:

try {
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("StringOne", stringOne));
    nameValuePairs.add(new BasicNameValuePair("StringTwo", stringTwo));
    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
}

In PHP:

<?php

 $stringOne = $_POST["StringOne"];
 $stringTwo = $_POST["StringTwo"];
?>
kelvincer
  • 5,929
  • 6
  • 27
  • 32
  • how to get the value in php then?? could you please provide the code for getting the value in php as well? thank you so much –  Jan 08 '15 at 15:22
0

Use this code snippet.

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("dataOne", "Hello"));
    nameValuePairs.add(new BasicNameValuePair("dataTwo", "Android World"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

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

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}   
BeingMIAkashs
  • 1,375
  • 11
  • 18
  • how to get the value in php then?? could you please provide the code for getting the value in php as well? thank you so much –  Jan 08 '15 at 15:23
  • import org.apache.http.client.HttpClient; cannot be resolved – eawedat Feb 15 '17 at 07:03
0

First of all, you need to implement an AsyncTask to configure this task. You can find out more on how to implement AsyncTask on Android Developers | Async Task.

Then in method doInBackground(String...strings) :

@Override
    protected String doInBackground(String... strings)  // Connect to the database, write query and add items to array list
    {

        OkHttpClient client = new OkHttpClient();
        okhttp3.Request request = new okhttp3.Request.Builder()
                .url("http://example.gr/yourscript.php?id="+id+
                        "&anotherParam="+param)  //send parameteres to php script
                .build();
        try {
            Response response = client.newCall(request).execute();
            String responseString = response.body().string();
            System.out.println(responseString);


        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return x;
    }

You should also have a php script where you will fetch the parameters you need to pass via GET. e.g. $id = $_GET['id'];