Php Post请求接收空值
我正在开发一个软件,其中我向服务器发送一个post请求,该请求是用PHP编写的,但服务器接收空值。任何帮助都将不胜感激 随函附上Android和PHP文件Php Post请求接收空值,php,android,Php,Android,我正在开发一个软件,其中我向服务器发送一个post请求,该请求是用PHP编写的,但服务器接收空值。任何帮助都将不胜感激 随函附上Android和PHP文件 **This is my Android file:** public static JSONObject tosend = new JSONObject(); for(int i=0;i<3;i++) {
**This is my Android file:**
public static JSONObject tosend = new JSONObject();
for(int i=0;i<3;i++)
{
JSONObject jo = new JSONObject();
jo.put("infoName", infoName[i]);
jo.put("infoNumber", infoNumber[i]);
jo.put("direction",direction[i]);
jo.put("version",version[i]);
JSONArray ja = new JSONArray();
ja.put(jo);
tosend .accumulate("introduceesJson", ja);
}
String link = "http://172.65.45.7/checkAppInstalled.php";
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost(link);
StringEntity se;
se = new StringEntity(tosend.toString());
// Set HTTP parameters
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
httpPostRequest.setEntity(se);
httpPostRequest.setHeader("Accept", "*/*");
httpPostRequest.setHeader("Content-type", "*/*");
//httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression
//long t = System.currentTimeMillis();
/* HttpPost httpPostRequest = new HttpPost(link);
List<NameValuePair> nVP = new ArrayList<NameValuePair>(2);
nVP.add(new BasicNameValuePair("json", tosend.toString()));
httpPostRequest.setEntity(new UrlEncodedFormEntity(nVP));*/
//Log.e("Posted data", "TestPOST - nVP = "+nVP.toString());
// Hand the NVP to the POST
Log.e("posted data",tosend.toString());
HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
if (response.getStatusLine().getStatusCode() == 200)
{
HttpEntity entity = response.getEntity();
String jsonReceived = EntityUtils.toString(entity);
Log.e("result received",jsonReceived);
}
以下是logcat结果:
虽然对json了解不多,但还是有点胡闹:试试这个:
var_dump($jsonObj['introduceesJson']);
$jsonArray = $jsonObj['introduceesJson'];
foreach($jsonArray as $json)
{
echo("------\n");
var_dump ($json);
foreach ( $json as $jsonarray )
{
echo(" ------\n");
var_dump ($jsonarray);
echo $jsonarray['infoName'];
echo $jsonarray['infoNumber'];
echo $jsonarray['direction'];
echo $jsonarray['version'];
echo ("\n");
}
} –
完成后,请发布完整的php脚本以提取所有变量,好吗?我很困惑……se=newstringetytosend.toString;不需要json=服务器接收空值。哪里哪一个变量为空?@Steve,如果我不使用json=,我将如何在php中接受请求?@Steve,例如,ifisset$\u POST[what?]@greenapps,在php中,$jsonString=file\u get\u contents'php://input'; 接收空值$jsonString=file\u get\u contents'php://input'; $jsonObj=json_decode$jsonString,true$arr=阵列$jsonArray=$jsonObj['introductesjson'];foreach$jsonArray as$json{foreach$json as&$item{if is_数组$item{foreach$item as$key=>$value数组\u push$arr,$value;}}}}}}print\u r$arr;谢谢这不会打印所有值。如果是_数组$item{foreach$item as$key=>$value数组_push$arr,$value;}否则为var_dump$item;如何添加到$arr?为什么第一个不同?是的,伙计。有一个问题。我不知道为什么第一个不同:
04-02 15:34:07.882 9526-9704/com.thoughtrix.introduce E/posted data﹕ {"introduceesJson":[{"infoName":"Aa","direction":"2","version":"1","infoNumber":"96 35 874125"},[{"infoName":"Aa","direction":"2","version":"1","infoNumber":"96 35 874125"}]]}
Result Received : json empty.
var_dump($jsonObj['introduceesJson']);
$jsonArray = $jsonObj['introduceesJson'];
foreach($jsonArray as $json)
{
echo("------\n");
var_dump ($json);
foreach ( $json as $jsonarray )
{
echo(" ------\n");
var_dump ($jsonarray);
echo $jsonarray['infoName'];
echo $jsonarray['infoNumber'];
echo $jsonarray['direction'];
echo $jsonarray['version'];
echo ("\n");
}
} –