我使用Android应用程序(java)将数据发送到mysql数据库并使用php进行传输。我正在使用HttpClient
发布它。下面是Http客户端的片段,它是在异步背景上完成的。
EDIT : This is how I am posting the the variables content
...
HashMap<String, String> map = new HashMap<String, String>();
map.put("url", "../register");
map.put("id", id.getText().toString());
map.put("name", name.getText().toString());
if (!TextUtils.isEmpty(image)) {
map.put("picture",image);
}
new MultiPartRequester(activity, map,1, this);
...
这是发射器
...
HttpPost httppost = new HttpPost(urls[0]);
httpclient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(
httpclient.getParams(), 200000);
MultipartEntityBuilder builder = MultipartEntityBuilder
.create();
for (String key : map.keySet()) {
if (key.equalsIgnoreCase(Params.Image)) {
File f = new File(map.get(key));
builder.addBinaryBody(key, f,
ContentType.MULTIPART_FORM_DATA, f.getName());
} else {
builder.addTextBody(key, map.get(key), ContentType.create("text/plain", MIME.UTF8_CHARSET));
}
}
httppost.setEntity(builder.build());
HttpResponse response = httpclient.execute(httppost);
String responseBody = EntityUtils.toString(
response.getEntity(), "UTF-8");
return responseBody;
...
现在,上面的客户端运行良好,但是在php文件上接收它后,变量返回null。这就是我用php接收它的方式。
<?php
$id=$_POST['id'];
$name=$_POST['name'];
$response["success"]="true";
$response["id"]=$id;
echo json_encode($response);
//The print out
response :{"success":"true","id":"null"}
?>
有什么我真的做错了,我很感激你的帮助。此致!