Android:HttpPost不起作用

时间:2013-12-10 12:05:48

标签: java php android json post

嗨伙计(抱歉我的英文错误:P)我有问题,我正在尝试将变量(id_art)发布到php页面,问题是我无法理解变量是否未发送正确的,或者如果我读错了php端。

JAVA CODE:

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

结果为NULL ...为什么????

Tnks 4帮助

编辑1

我尝试编辑我的PHP代码替换

this:json_decode(stripslashes($ _ POST),true);

with:$ value = json_decode($ _ POST);

但结果是一样的.. NULL

编辑2 我试着替换

在.JAVA

httpPost.setEntity(new StringEntity(yourJson.toString(),“UTF-8”));

in .PHP $ value = json_decode(file_get_contents('php:// input')); echo $ value;

但结果为NULL

3 个答案:

答案 0 :(得分:0)

在.JAVA

httpPost.setEntity(new StringEntity(yourJson.toString(),"UTF-8"));

in .PHP

$value = file_get_contents('php://input');
var_dump(json_decode($value , true));

答案 1 :(得分:0)

试试这个 在.JAVA

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);  
nameValuePairs.add(new BasicNameValuePair("json", yourJson.toString()));  
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

in .PHP

$value = $_POST['json'];
var_dump(json_decode($value , true));

答案 2 :(得分:0)

我相信你不能简单地发送StringEntity,因为POST参数应该是key =&gt;值对。这意味着您需要为参数指定名称,例如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);

在PHP方面,你只需要做

$value = json_decode($_POST['json'], true);
var_dump($value);