AnyBody,請解決我的問題。我無法從凌空StringRequest.What任何POST值是在我的代碼問題沒有從排氣字符串獲取JSON 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( 「響應」,響應);'什麼是日誌?而你不是發送/接收json –
你的代碼從android側看起來不錯你能告訴我的php代碼 – adam
你是否得到了迴應?請包括它 – 2017-04-17 11:02:01