开发者

HTTP post from android to PHP not working

I am trying to get a Android device to send some information to a local host. I believe I have the Android sending the information, but my PHP code is not accepting or not displaying the code. I have attached my code, is there something I have missed? I am running wamp server also, and have put the permissions into the manifest.

Java Code: #

HttpPost httppost;

    HttpClient httpclient;

    // List with arameters and their values
    List<NameValuePair> nameValuePairs;

    String serverResponsePhrase;
    int serverStatusCode;
    String bytesSent;
    String serverURL = "http://10.0.2.2/test/index.php";


    httppost = new HttpPost(serverURL);
    httpclient = new DefaultHttpClient();
    nameValuePairs = new ArrayList<NameValuePair>(2);

    // Adding parameters to send to the HTTP server.
    nameValuePairs.add(new BasicNameValuePair("parameterName1", "git"));
    nameValuePairs.add(new BasicNameValuePair("parameterName2", "git"));

    // Send POST message with given parameters to the HTTP server.
    try {
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpclient.execute(httppost);

        InputStream is = response.getEntity().getContent();
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayBuffer baf = new ByteArrayBuffer(20);

        int current = 0;
        while ((current = bis.read()) != -1) {
            baf.append((byte) current);
        }

        bytesSent = new String(baf.toByteArray());

        // Response from the server
        serverResponsePhrase = response.getStatusLine().getReasonPhrase();
        serverStatusCode = response.getStatusLine().getStatusCode();
        System.out.println("COMPLETE");
    } catch (Exception e) {
        // Exception handling
        System.out.println("Problem is " + e.toString());
    }

PHP Code:

<?php
echo "param1 value: ".$_POST['parameterName1']."\n";
echo "param2 value: ".$_POST['parameterName2']."\n";
?>

I also tried this code, but it did not work with my PHP

HttpPost httppost;

    HttpClient httpclient;

    // List with arameters and their values
    List<NameValuePair> nameValuePairs;

    String serverResponsePhrase;
    int serverStatusCode;
    String bytesSent;
    String serverURL = "http://10.0.2.2/test/index.php";


    httppost = new HttpPost(serverURL);
    httpclient = new DefaultHttpClient();
    nameValuePairs = new ArrayList<NameValuePair>(2);

    // Adding parameters to send to the HTTP server.
    nameValuePairs.add(new BasicNameValuePair("'parameterName1'", "git"));
    nameValuePairs.add(new BasicNameValuePair("'parameterName2'", "git"));

    // Send POST message with given parameters to the HTTP server.
    try {
        HttpEntity entity = new UrlEncodedFormEntity(nameValuePairs);

        httppost.addHeader(entity.getContentType());
        httppost.setEntity(entity);


        HttpResponse response 开发者_开发问答= httpclient.execute(httppost);

        InputStream is = response.getEntity().getContent();
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayBuffer baf = new ByteArrayBuffer(20);

        int current = 0;
        while ((current = bis.read()) != -1) {
            baf.append((byte) current);
        }

        bytesSent = new String(baf.toByteArray());

        // Response from the server
        serverResponsePhrase = response.getStatusLine().getReasonPhrase();
        serverStatusCode = response.getStatusLine().getStatusCode();
        System.out.println("response" + response.toString());
        System.out.println("COMPLETE");
    } catch (Exception e) {
        // Exception handling
        System.out.println("Problem is " + e.toString());
    }


Make sure the Content-Type HTTP header is getting set. Try replacing

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

with this

HttpEntity entity = new UrlEncodedFormEntity(nameValuePairs);

httppost.addHeader(entity.getContentType());
httppost.setEntity(entity);


Also, instead of response.toString(), try EntityUtils.toString(response.getEntity()) if you want to see the body of the response

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜