0

次のコードは、Android から PHP に文字列を POST しようとします。

  • INTERNET パーミッションが与えられます。
  • サメはチェック済みです。
  • デバイスは MAMP (WAMP for MAC) サーバーに接続し、文字列を送信します。

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"]; 
4

2 に答える 2

0

やってみました:

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

?

于 2013-01-09T17:17:26.620 に答える
0

実際にデータを投稿していると確信していますか?

以下のメソッドのように 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();
    }
}
于 2013-01-11T10:49:14.367 に答える