Java Android json发送数据
我正在尝试发送json(java Android): 这是完整的代码Java Android json发送数据,java,android,json,post,Java,Android,Json,Post,我正在尝试发送json(java Android): 这是完整的代码 HttpPost httppost = new HttpPost("http://example.com"); try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("login", "qwerty")); na
HttpPost httppost = new HttpPost("http://example.com");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("login", "qwerty"));
nameValuePairs.add(new BasicNameValuePair("password", "123457"));
HttpEntity entity = new UrlEncodedFormEntity(nameValuePairs);
httppost.setEntity(entity);
// Headers
httppost.setHeader("Content-Type", "application/json");
HttpResponse response = new DefaultHttpClient().execute(httppost);
responseString = EntityUtils.toString(response.getEntity());
System.out.println(responseString);
return responseString;
} catch (Exception e) {
System.out.println("error");
return "";
}
HttpPost-HttpPost=新的HttpPost(“http://example.com");
试一试{
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“登录”、“qwerty”);
添加(新的BasicNameValuePair(“密码”,“123457”);
HttpEntity实体=新的UrlEncodedFormEntity(nameValuePairs);
httppost.setEntity(实体);
//标题
setHeader(“内容类型”、“应用程序/json”);
HttpResponse response=new DefaultHttpClient().execute(httppost);
responseString=EntityUtils.toString(response.getEntity());
系统输出打印LN(响应预算);
回报率;
}捕获(例外e){
System.out.println(“错误”);
返回“”;
}
服务器php代码:
<?php
$response[] = array(
'postcount' => count($_POST), // Get count $_POST
'get' => count($_GET),
'request' => count($_REQUEST)
);
echo json_encode($response);
?>
System.out.println(request) - // And i get [{"postcount":null,"get":null,"request":null}]
System.out.println(请求)-//我得到[{“postcount”:null,“get”:null,“request”:null}]
请求发送成功,但参数丢失。
请帮忙:)但你的目标是什么?你想要什么?你在哪里结巴?请mentoin@AndroidDev我想发送带参数的请求。你不想发送带参数的请求吗?在此代码中,请求已成功发送。但是没有参数。好的,告诉我你对输出的期望是什么,你对url的期望是什么?你想要get还是post方法?