从android到PHP的HTTP帖子不起作用

时间:2011-09-21 15:27:30

标签: php android

我正在尝试让Android设备向本地主机发送一些信息。我相信我有Android发送信息,但我的PHP代码不接受或不显示代码。我附上了我的代码,有什么我错过了吗?我也在运行wamp服务器,并已将权限放入清单。

Java代码:#

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代码:

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

我也尝试过这段代码,但它不适用于我的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());
    }

1 个答案:

答案 0 :(得分:0)

确保Content-Type HTTP标头已设置。尝试替换

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

用这个

HttpEntity entity = new UrlEncodedFormEntity(nameValuePairs);

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

<小时/> 另外,如果您想查看回复正文

,请尝试response.toString(),而不是EntityUtils.toString(response.getEntity())