AnyBody,请解决我的问题。我无法从凌空StringRequest获取任何POST值。我的代码中的问题是什么
这是我的Android代码
StringRequest stringRequest = new StringRequest(Request.Method.POST,URL,
new Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
Log.d("response",response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error)
{
Toast.makeText(PostRequestTest.this, error.toString(), Toast.LENGTH_LONG).show();
}
})
{
//getaParams function is used to send the values to the server
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<>();
params.put("user_name","uname");
Log.d("parameters09876", params.toString());
JSONObject jsonObject = new JSONObject(params);
Log.d("jsonRequest",jsonObject.toString());
return params;
}
};
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
Config.MY_SOCKET_TIMEOUT_MS,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
CustomVolleyRequest.getInstance(this).addToRequestQueue(stringRequest);
}
在我的PHP代码中,我只需打印POST值
<?php echo $_POST['user_name']; ?>
这是我的CustomVolleyRequest
public class CustomVolleyRequest {
private static CustomVolleyRequest customVolleyRequest;
private static Context context;
private RequestQueue requestQueue;
private ImageLoader imageLoader;
private CustomVolleyRequest(Context context) {
this.context = context ;
this.requestQueue = getRequestQueue();
imageLoader = new ImageLoader(requestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized CustomVolleyRequest getInstance(Context context) {
if (customVolleyRequest == null) {
customVolleyRequest = new CustomVolleyRequest(context);
}
return customVolleyRequest;
}
public RequestQueue getRequestQueue() {
if (requestQueue == null) {
Cache cache = new DiskBasedCache(context.getCacheDir(), 10 * 1024 * 1024);
Network network = new BasicNetwork(new HurlStack());
requestQueue = new RequestQueue(cache, network);
requestQueue.start();
}
return requestQueue;
}
public <T> void addToRequestQueue(Request<T> req)
{
getRequestQueue().add(req);
}
public ImageLoader getImageLoader()
{
return imageLoader;
}
}
最佳答案
我给你一个建议。使用JsonObjectRequest
而不是使用StringRequest
。StringRequest
:指定URL并接收原始字符串作为响应。JsonObjectRequest
和JsonArrayRequest
(JsonRequest的两个子类)。指定一个URL并分别获取一个JSON对象或数组作为响应。
TextView mTxtDisplay;
ImageView mImageView;
mTxtDisplay = (TextView) findViewById(R.id.txtDisplay);
String url = "http://my-json-feed";
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
mTxtDisplay.setText("Response: " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(jsObjRequest);
See this docs
关于php - 无法从 Volley stringRequest获取JSON POST值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43449853/