其实我关注了很多stackoverflow答案与我的问题相关,但没有一个答案有效,我只是发布一个字符串,作为返回,我想要一个 json 数组
注意:当我运行我的硬代码脚本时,它非常好,但在脚本中,POST 值显示为 null 这是我的 Android 代码:
private void getData(){
Bundle extras=getIntent().getExtras();
final String id = extras.getString("value").toString().trim();
JSONObject obj =new JSONObject();
final ProgressDialog loading = ProgressDialog.show(Categories.this, "Please wait...","Fetching data...",false,false);
Volley.newRequestQueue(this).add(new JsonRequest<JSONArray>(Request.Method.POST, CATEGORIES_URL, obj.toString(),
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
loading.dismiss();
showList(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
loading.dismiss();
Toast.makeText(getApplicationContext(),
"Ooops!,Internet Connection Problem", Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put(KEY_ID,id);
return super.getParams();
}
@Override
protected Response<JSONArray> parseNetworkResponse(
NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser
.parseCharset(response.headers));
return Response.success(new JSONArray(jsonString),
HttpHeaderParser
.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
});
// RequestQueue requestQueue = Volley.newRequestQueue(this);
// requestQueue.add(jsonArrayRequest);
Toast.makeText(Categories.this,id,Toast.LENGTH_LONG ).show();
}
在服务器端我使用 $id=$_POST['id'];
但它显示为空
不知道是什么问题
我的 PHP 脚本:
<?php
require_once('dbConnect.php');
$json = file_get_contents('php://input');
$stripe_json= json_decode($json, TRUE);
$ida=$stripe_json->id;
$sql= "select title,description,image,price,cid FROM products a where a.cid='".$ida."'";
$res=mysqli_query($con,$sql);
$result = array();
while ($row=mysqli_fetch_array($res)){
array_push($result,array('title'=>$row['0'],
'description'=>$row['1'],
'image'=>$row['2'],
'price'=>$row['3'],
));
}
echo json_encode(($result));
mysqli_close($con);
?>
最佳答案
或许您可以为此使用 HTTPPost。可能是因为 cookie 的东西,只需要添加这个:
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
httpPost.setCookieHandler(cookieManager);
希望对您有所帮助!
关于php - JSONArray 中的 POST 字符串和响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39951257/