【发布时间】:2017-09-12 23:24:18
【问题描述】:
AnyBody,请解决我的问题。我无法从 volley 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;
}
}
【问题讨论】:
-
Log.d("response",response);什么是日志?而你没有发送/接收 json -
你的android端代码看起来不错,能不能给我看看php代码
-
您收到回复了吗?请包括它
-
我得到了 POST 值作为我的响应。只是想显示 POST 值。但 POST 值未显示 @LManoj
-
在 PHP 中我只是打印 POST 值。我已经在我的问题@adam 中展示了我的 PHP 代码
标签: php android json android-studio android-volley