android发送json数据到app,使用JSON数据将数据从Android发送到服务器

我正在尝试将数据从Android应用程序发送到Web服务器。我的android应用程序正在成功工作。但是PHP代码有问题。使用JSON数据将数据从Android发送到服务器

$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服务器)

下面是相关的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

}

此代码工作顺利在Android上侧(无误差)。但PHP方面有问题.. 谢谢。

2011-11-29

iremce

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值