加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

使用Volley with JsonObjectRequest时无法从PHP获取参数

发布时间:2020-12-13 17:45:12 所属栏目:PHP教程 来源:网络整理
导读:我正在使用Volley框架和JsonObjectRequest请求.我在用 JsonObjectRequest loginRequest = new JsonObjectRequest(b.toString(),params,new ListenerJSONObject() {},new Response.ErrorListener() {}); params变量包含参数,它是一个JSONObject. 问题是我无法
我正在使用Volley框架和JsonObjectRequest请求.我在用

JsonObjectRequest loginRequest = new JsonObjectRequest(b.toString(),params,new Listener<JSONObject>() {},new Response.ErrorListener() {});

params变量包含参数,它是一个JSONObject.

问题是我无法在PHP代码中访问任何这些变量. $_POST或$_REQUEST变量什么都没给我.

我也尝试过类似下面的东西,但没有运气.

$data = json_decode(file_get_contents("php://input"));

解决方法

我在使用我的PHP API时遇到了同样的问题.事实证明,params的JSONObject作为JSON发送.因此,PHP $_POST将无法识别它,因为它不是格式:param1 = value1& param2 = value

为了自己看看,请尝试:print file_get_contents(“php:// input”);

我的解决方案是创建一个扩展Request< JSONObject>的解决方法类.而不是使用JsonObjectRequest

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.toolbox.HttpHeaderParser;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Custom Request Class for JSON
*/
public class CustomRequest extends Request<JSONObject> {

    private List params; // the request params
    private Response.Listener<JSONObject> listener; // the response listener

    public CustomRequest(int requestMethod,String url,List params,Response.Listener<JSONObject> responseListener,Response.ErrorListener errorListener) {

        super(requestMethod,url,errorListener); // Call parent constructor
        this.params = params;
        this.listener = responseListener;
    }

    // We have to implement this function
    @Override
    protected void deliverResponse(JSONObject response) {
        listener.onResponse(response); // Call response listener
    }

    // Proper parameter behavior
    @Override
    public Map <String,String> getParams() throws AuthFailureError {
        Map <String,String> map = new HashMap <String,String>();

        // Iterate through the params and add them to our HashMap
        for (BasicNameValuePair pair: params) {
            map.put(pair.getName(),pair.getValue());
        }

        return map;
    }

    // Same as JsonObjectRequest#parseNetworkResponse
    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data,HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONObject(jsonString),HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读