是否可以使用 Volley 发送 JSON 对象而不是 HASH MAP<字符串、字符串>?



我可以将Map <String, String> POST到我的服务器,但它是以&分隔的形式提供的。

我使用了来自使用Volley发送帖子请求和在PHP 中接收的代码

getParams()只是不适用于JSONObject返回类型。是否可以仅将JSONObject作为JSON发送?

我想以JSON的形式发送数据,我将使用file_get_contents(php://input)获得该数据。为此,我已将Content-Type更改为application/json; charset=utf-8

问题是使用这种方式,我获得x=abc&y=def格式的数据,因为它是Map<String, String>类型,并且我想要{"x":"abc", "y":"def"}JSON格式的数据

这与上面的问题不同,因为我想在JSON ONLY中发布数据,而不是在字符串

MAP中发布数据

试试这个:

private void jsonObjReq() {
        showProcessDialog();
        Map<String, String> postParam= new HashMap<String, String>();
        postParam.put("un", "xyz@gmail.com");
        postParam.put("p", "somepasswordhere");

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
        Const.URL_LOGIN, new JsonObject(postParam),
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.d(TAG, response.toString());
                msgResponse.setText(response.toString());
                hideProgressDialog();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                hideProgressDialog();
            }
        }) {
    /**
     * Passing some request headers
     * */
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json; charset=utf-8");
        return headers;
    }

};

更改php中的头。

简单的方法是这个

final RequestQueue requestQueue = Volley.newRequestQueue(this);
final String url ="http://mykulwstc000006.kul/Services/Customer/Register";
Map<String, String>  params = new HashMap<String, String>();
params.put("MobileNumber", "+97333765439");
params.put("EmailAddress", "danish.hussain4@das.com");
params.put("FirstName", "Danish2");
params.put("LastName", "Hussain2");
params.put("Country", "BH");
params.put("Language", "EN");
JsonObjectRequest req = new JsonObjectRequest(url, new JSONObject(params),
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    VolleyLog.v("Response:%n %s", response.toString(4));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        VolleyLog.e("Error: ", error.getMessage());
    }
}){
    @Override
    public String getBodyContentType() {
        return "application/json; charset=utf-8";
    }
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        String username ="eli@gmail.com";
        String password = "elie73";
        String auth =new String(username + ":" + password);
        byte[] data = auth.getBytes();
        String base64 = Base64.encodeToString(data, Base64.NO_WRAP);
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization","Basic "+base64);
        return headers;
    }
};
requestQueue.add(req);

}

相关内容

最新更新