【问题标题】:Cannot get parameters from PHP when using Volley with JsonObjectRequest使用带有 JsonObjectRequest 的 Volley 时无法从 PHP 获取参数
【发布时间】:2013-06-22 23:32:45
【问题描述】:

我正在使用带有 JsonObjectRequest 请求的 Volley 框架。我正在使用

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 android json


    【解决方案1】:

    在我的 PHP API 中使用 Volley 时遇到了同样的问题。结果,JSONObject 的参数作为 JSON 发送。因此,PHP $_POST 不会识别它,因为它的格式不是:param1=value1&amp;param2=value

    要亲眼看看:print file_get_contents("php://input");

    我的解决方案是创建一个扩展 Request&lt;JSONObject&gt; 的解决方法类,而不是使用 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));
            }
        }
    
    }
    

    【讨论】:

    • 是的,不幸的是,这就是问题所在。它确实解决了您的自定义请求类的问题。
    • 您的博文链接已失效。 :(
    • @NightlyNexus 对此感到抱歉,现在应该修复了!
    • @kxc1013 你救了我的命..哈哈哈
    猜你喜欢
    • 2021-06-21
    • 2018-10-24
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多