从Android的发送数据数据、Android

2023-09-05 23:08:59 作者:日久生情、爱久生厌

我想从Android设备到PHP发送的数据。我曾经试过,但我在哪里错了,我不知道。我该如何纠正呢?

I want to sent the data from an Android device to PHP. I have tried but where I am wrong I don't know. How do I correct this?

这是Android code,我已经做了。

This is the Android code that I have done.

public class php_connect extends Activity {
    InputStream is;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);
        JSONObject json = new JSONObject();
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://10.0.2.2/food/food.php");
            json.put("id",1);
            json.put("name","john");
            Log.i("jason Object", json.toString());
            httppost.setHeader("json", json.toString());

            StringEntity se = new StringEntity(json.toString());
            se.setContentEncoding("UTF-8");
            se.setContentType("application/json");
            httppost.setEntity(se);
            HttpResponse response = httpclient.execute(httppost);
            int statusCode = response.getStatusLine().getStatusCode();
            Toast.makeText(getApplicationContext(),String.valueOf(statusCode), Toast.LENGTH_SHORT).show();
            is = se.getContent();
            Log.e("log_tag", "connection success ");
            Toast.makeText(getApplicationContext(), "Successfully Connected", Toast.LENGTH_SHORT).show();
        }
        catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
            Toast.makeText(getApplicationContext(), "Fail to Connect", Toast.LENGTH_SHORT).show();
        }
    }
}

我得到的响应是200和连接成功,但不能看到PHP数据。

I get response as 200 and connected successfully but cannot see data in PHP.

和我的PHP code是:

And my PHP code is:

<?php
    $data = file_get_contents('php://input');
    $json = json_decode($data,true);
    var_dump($json);
    $id=$json['id'];
    echo $id;
?>

我总是空的结果。

I always get the null result.

推荐答案

从 PHP手册:

在POST请求来说,它preferrable至$ HTTP_RAW_POST_DATA,因为它不依赖于特殊的php.ini设置。此外,对于不填充默认情况下是在哪里$ HTTP_RAW_POST_DATA这种情况下,这是一种潜在的更少的内存密集型的替代激活always_populate_raw_post_data。 PHP://输入不适用于ENCTYPE =的multipart / form-data的

In case of POST requests, it preferrable to $HTTP_RAW_POST_DATA as it does not depend on special php.ini directives. Moreover, for those cases where $HTTP_RAW_POST_DATA is not populated by default, it is a potentially less memory intensive alternative to activating always_populate_raw_post_data. php://input is not available with enctype="multipart/form-data"

我认为这将是更容易连接code你的JSON作为一个表单参数,并使用普通的PHP $ _ POST功能来阅读:

I think it will be even easier to encode your JSON as a form parameter and use the normal PHP $_POST function to read it:

List parameters = new ArrayList();
parameters.add(new BasicNameValuePair("jsonParam", json.toString())
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters)
httppost.setContentType("multipart/form-data");
httppost.setEntity(formEntity);   

在PHP端,你可以简单地做

On the PHP side you can simply do

Got <?php echo $_POST["jsonParam"]; ?>!<br />

注:我没有测试过上述code

Note: I have not tested the above code.