简体   繁体   English

使用JSON数据从Android发送数据到服务器

[英]Sending Data From Android To Server with JSON data

I am trying sending data from Android application to web server. 我正在尝试将数据从Android应用程序发送到Web服务器。 My android application is working successfully.However php code have problems. 我的android应用程序正在成功运行。但是php代码有问题。

<?php
$json = $_SERVER['HTTP_JSON'];
echo "JSON: \n";
var_dump($json);
echo "\n\n";

$data = json_decode($json,true);
echo "Array: \n";
var_dump($data);
echo "\n\n";

$name = $data['name'];
$pos = $data['position'];
echo "Result: \n";
echo "Name     : ".$name."\n Position : ".$pos; 
?>

Errors: 错误:

Notice: Undefined index: HTTP_JSON in C:\wamp\www\jsonTest.php on line 2
( line 2 : $json = $_SERVER['HTTP_JSON']; )

I couldn't find these problems reason. 我找不到这些问题的原因。 Can you help me ? 你能帮助我吗 ? ( note: I am using wamp server ) (注意:我正在使用wamp服务器)

Here is the relevant Android source: 以下是相关的Android来源:

// Create a new HttpClient and Post Header 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("10.0.2.2:90/jsonTest.php";); 

JSONObject json = new JSONObject(); 
try { 
    json.put("name", "flower"); 
    json.put("position", "student"); 
    JSONArray postjson=new JSONArray(); 
    postjson.put(json); 
    httppost.setHeader("json",json.toString());
    httppost.getParams().setParameter("jsonpost",postjson); 
    System.out.print(json); 
    HttpResponse response = httpclient.execute(httppost); 

    if(response != null)
    {
    InputStream is = response.getEntity().getContent();

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        } catch (IOException e) {
        e.printStackTrace();
        } finally {
        try {
        is.close();
        } catch (IOException e) {
        e.printStackTrace();
        }
        }
    text = sb.toString();
    }
    tv.setText(text);

}catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}

This code works successfully on android side(no error). 此代码在android端成功运行(无错误)。 But php side has problems.. Thanks. 但是php方面有问题..谢谢。

This isn't where your JSON is: 这不是你的JSON所在的地方:

$json = $_SERVER['HTTP_JSON'];

You possibly meant: 你可能意味着:

$json = $_POST['HTTP_JSON'];

Where HTTP_JSON is the POST variable name you gave to your JSON in your Android app. 其中HTTP_JSON是您在Android应用中为JSON提供的POST变量名称。

The rest of the errors stem from the fact that json_decode is failing because you're not successfully reading the JSON data from the request. 其余的错误源于json_decode失败的事实,因为您没有成功地从请求中读取JSON数据。 You can check the response of json_decode to check if it was successful as follows: 您可以检查json_decode的响应以检查它是否成功,如下所示:

$data = json_decode($json,true);
if( $data === NULL)
{
    exit( 'Could not decode JSON');
}

Finally, passing true as the second parameter to json_encode means it will return an associative array, so you'd access elements like so: 最后,将true作为第二个参数传递给json_encode意味着它将返回一个关联数组,因此你可以像这样访问元素:

$name = $data['name'];
$pos = $data['position'];

Make sure you read the docs for json_encode so you understand what it's doing. 请务必阅读 json_encode 的文档 ,以便了解它正在做什么。

Edit: Your problem is that you're accessing the $_POST parameter by the wrong name. 编辑:您的问题是您使用错误的名称访问$_POST参数。 You should be using: 你应该使用:

$json = $_POST['jsonpost'];

Since the following line names the parameter "jsonpost": 由于以下行命名参数“jsonpost”:

httppost.getParams().setParameter("jsonpost",postjson);

Since I don't know how the java client sends the request I would try : 由于我不知道java客户端如何发送请求,我会尝试:

print_r($_SERVER);
print_r($_GET);
print_r($_POST);

To figure out how it does. 弄清楚它是如何做的。

try these lines: 尝试这些线:

httppost.setHeader("Accept", "application/json");
     httppost.setHeader("Content-type", "application/json"); 

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

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