简体   繁体   English

Android:HttpPost无法正常工作

[英]Android: HttpPost not work

Hi guy's (sorry for my english error :P ) i have a problem, I'm trying to post a variable (id_art) to a php page, the problem is that I can't understand if the variable is not sent properly, or if I read it wrong php side. 嗨,伙计们(对不起我的英语错误:P),我有一个问题,我正在尝试将变量(id_art)发布到php页面,问题是我无法理解该变量是否未正确发送,或者如果我读错了PHP方面。

JAVA CODE: JAVA代码:

 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 CODE PHP代码

<?php
include_once('configurazione.php');
header("Content-Type: application/json");
mysql_set_charset('utf8');

$value = json_decode(stripslashes($_POST),true);
var_dump($value);
?>

result is NULL... Why ???? 结果为NULL ...为什么????

Tnks 4 help Tnks 4帮助

EDIT 1 编辑1

I try to edit my php code replacing 我尝试编辑我的PHP代码替换

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

with: $value = json_decode($_POST); 其中:$ value = json_decode($ _ POST);

But the result is the same.. NULL 但是结果是一样的。

EDIT 2 I try to replace 编辑2我尝试取代

in .JAVA 在.JAVA中

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

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

but result is NULL 但结果为NULL

in .JAVA 在.JAVA中

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

in .PHP 在.PHP中

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

try with this in .JAVA 尝试在.JAVA中使用

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

in .PHP 在.PHP中

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

I believe you cannot simply send StringEntity , because POST parameters are expected to be key=>value pairs. 我相信您不能简单地发送StringEntity ,因为POST参数应该是key => value对。 That means you need to give a name to your parameter, let's say json . 这意味着您需要给参数命名,例如json

Then you can do this: 然后,您可以执行以下操作:

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

On the PHP side, you'll just do 在PHP方面,您只需

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM