Java Android:HttpPost不工作
Hi guy为我的英语错误感到抱歉:p我有一个问题,我正试图将一个变量id_art发布到php页面,问题是我无法理解变量是否发送不正确,或者我是否在php端错误地读取了它 JAVA代码:Java Android:HttpPost不工作,java,php,android,json,post,Java,Php,Android,Json,Post,Hi guy为我的英语错误感到抱歉:p我有一个问题,我正试图将一个变量id_art发布到php页面,问题是我无法理解变量是否发送不正确,或者我是否在php端错误地读取了它 JAVA代码: HttpClient httpclient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(myurl); StringBuilder builder = new StringBuilder(); String json, resu
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(myurl);
StringBuilder builder = new StringBuilder();
String json, result = "";
//Build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("id_articolo", id_art);
//Convert JSONObject to JSON to String
json = jsonObject.toString();
//Set json to StringEntity
StringEntity se = new StringEntity(json);
//Set httpPost Entity
httpPost.setEntity(se);
//Set some headers to inform server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
//Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
//Receive response as inputStream
StatusLine statusLine = httpResponse.getStatusLine();
int statusCode = statusLine.getStatusCode();
//Convert input stream to string
if (statusCode == 200){
HttpEntity entity = httpResponse.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line="";
while ((line = reader.readLine()) != null) {
builder.append(line);
result = builder.toString();
}
System.out.println("DEBUG"+" "+result);
PHP代码
<?php
include_once('configurazione.php');
header("Content-Type: application/json");
mysql_set_charset('utf8');
$value = json_decode(stripslashes($_POST),true);
var_dump($value);
?>
结果为空。。。为什么
TNKs4帮助
编辑1
我试图编辑我的php代码 这是:json\u decodeStripsalshes$\u POST,true 使用:$value=json\u decode$\u POST 但结果是一样的。。空的 编辑2 我试着替换 in.JAVA
httpPost.setEntity(new StringEntity(yourJson.toString(),"UTF-8"));
httpPost.setEntitynew StringEntityyourJson.toString,UTF-8
in.PHP
$value=json\u decodefile\u get\u contents'php://input';
echo美元价值
但在.JAVA中,结果是空的
httpPost.setEntity(new StringEntity(yourJson.toString(),"UTF-8"));
in.PHP
$value = file_get_contents('php://input');
var_dump(json_decode($value , true));
$value = $_POST['json'];
var_dump(json_decode($value , true));
试试这个
in.JAVA
我相信您不能简单地发送StringEntity,因为POST参数应该是key=>value对。这意味着您需要为参数命名,比如json 然后你可以这样做:
JSONObject jsonObject = new JSONObject();
// here you can set up the data
HttpPost httppost = new HttpPost(URL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("json", jsonObject.toString()));
// here you can add more POST data using nameValuePairs.add()
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
$\u POST是数组,这就是原因。stripslashes用于stringphpinfo,当您需要知道服务器正在接收什么时,它是您的朋友。这太过分了,但是它会告诉你你的JSON是否会被发送到哪里;使用:$value=json\u decode$\u POST;但结果是一样的。。我的第一个答案有效?我尝试了所有答案,但没有人有效。。。我不明白怎么了!
$value = json_decode($_POST['json'], true);
var_dump($value);