繁体   English   中英

PHP无法从Android接收字符串

[英]PHP cannot receive a String from Android

以下代码尝试将字符串从Android POST到PHP。

  • 授予INTERNET权限。
  • 鲨鱼已经过检查。
  • 设备连接到MAMP(MAC的WAMP)服务器并发送字符串。

Android上的代码:

HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://MAMP-SERVER/post.php");

try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("username", "Andro"));
    post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    httpclient.execute(post);
    Log.i("POSTED", "YOUR USERNAME");

} catch (ClientProtocolException e) {
    Log.e("Client Protocol Exception", "Err "+ e.getMessage());
} catch (IOException e) {
    Log.e("IO Exception","Err "+ e.getMessage());
}

PHP上的代码:

$test=$_REQUEST["username"];  print $_REQUEST["username"]; 

你有没有尝试过:

$test=$_POST["username"];  print $_POST["username"]; 

您确定要发布数据吗?

您可以尝试发送POST请求,如下所示。 然后,您应该可以简单地通过$ _POST在PHP中获得您的价值

public void execute(final RequestMethod method)
        throws IllegalArgumentException, UnsupportedEncodingException {
    switch (method) {
    case GET:
        // add parameters
        String combinedParams = "";
        if (!params.isEmpty()) {
            combinedParams += "?";
            for (NameValuePair p : params) {
                String paramString = p.getName() + "="
                        + URLEncoder.encode(p.getValue(), "UTF-8");

                if (combinedParams.length() > 1) {
                    combinedParams += "&" + paramString;
                } else {
                    combinedParams += paramString;
                }
            }
        }
        HttpGet getRequest = new HttpGet(remoteUrl + combinedParams);
        // add headers
        for (NameValuePair h : headers) {
            getRequest.addHeader(h.getName(), h.getValue());
        }
        executeRequest(getRequest, remoteUrl);
        break;
    case POST:
        HttpPost postRequest = new HttpPost(remoteUrl);
        // add headers
        for (NameValuePair h : headers) {
            postRequest.addHeader(h.getName(), h.getValue());
        }
        if (!params.isEmpty()) {
            postRequest.setEntity(new UrlEncodedFormEntity(
                    (List<? extends NameValuePair>) params, HTTP.UTF_8));
        }
        executeRequest(postRequest, remoteUrl);
        break;
    default:
        break;
    }
}

private void executeRequest(final HttpUriRequest request, final String url) {
    HttpClient client = new DefaultHttpClient();

    HttpResponse httpResponse;

    try {
        httpResponse = client.execute(request);
        responseCode = httpResponse.getStatusLine().getStatusCode();
        errorMessage = httpResponse.getStatusLine().getReasonPhrase();

        HttpEntity entity = httpResponse.getEntity();

        if (entity != null) {

            responseStream = entity.getContent();

            if (!needStreamInsteadOfString) {
                response = convertStreamToString(responseStream);

                // Closing the input stream will trigger connection release
                responseStream.close();
            }
        }

    } catch (ClientProtocolException e) {
        client.getConnectionManager().shutdown();
        e.printStackTrace();
    } catch (IOException e) {
        client.getConnectionManager().shutdown();
        e.printStackTrace();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM