使用JSON数据发送数据从安卓到服务器 [英] Sending Data From Android To Server with JSON data

查看:237
本文介绍了使用JSON数据发送数据从安卓到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Android应用程序与Web服务器试图发送数据。我的Andr​​oid应用程序正在successfully.However PHP code有问题。

I am trying sending data from Android application to web server. My android application is working successfully.However php code have problems.

<?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; 
?>

错误:

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

我无法找到这些问题的原因。你能帮助我吗 ? (注:我使用WAMP服务器)

I couldn't find these problems reason. Can you help me ? ( note: I am using wamp server )

下面是有关Android源:

Here is the relevant Android source:

// 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
}

这code成功的作品在Android上侧(无误差)。但是PHP端有问题.. 谢谢你。

This code works successfully on android side(no error). But php side has problems.. Thanks.

推荐答案

这是不是在您的JSON是:

This isn't where your JSON is:

$json = $_SERVER['HTTP_JSON'];

您可能意味着:

$json = $_POST['HTTP_JSON'];

其中, HTTP_JSON 是你在你的Andr​​oid应用程序给你JSON的POST变量名。

Where HTTP_JSON is the POST variable name you gave to your JSON in your Android app.

错误的其余部分源于一个事实,即 json_de code 失败,因为你没有成功地从请求读取JSON数据。您可以检查 json_de $ C $了C 的反应来检查是否成功如下:

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. You can check the response of json_decode to check if it was successful as follows:

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

最后,通过作为第二个参数 json_en code 意味着它会返回一个关联数组,所以你访问的元素,像这样:

Finally, passing true as the second parameter to json_encode means it will return an associative array, so you'd access elements like so:

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

请确保您阅读文档为json_en code让你明白它做。

Make sure you read the docs for json_encode so you understand what it's doing.

编辑:您的问题是,你被错误的名称访问 $ _ POST 参数。你应该使用:

Your problem is that you're accessing the $_POST parameter by the wrong name. You should be using:

$json = $_POST['jsonpost'];

由于以下行命名的参数jsonpost:

Since the following line names the parameter "jsonpost":

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

这篇关于使用JSON数据发送数据从安卓到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆