【问题标题】:Is it possible to send JSON Object instead of HASH MAP <String, String> using Volley?是否可以使用 Volley 发送 JSON 对象而不是 HASH MAP <String, String>?
【发布时间】:2023-03-31 14:45:02
【问题描述】:

我可以将Map &lt;String, String&gt; 发布到我的服务器,但它以&amp; 分隔的形式出现。

我使用了Send post request using Volley and receive in PHP的代码

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

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

问题是使用这种方式我以x=abc&amp;y=def 格式获取数据,因为它是Map&lt;String, String&gt; 类型,我想要JSON 格式{"x":"abc", "y":"def"} 的数据

这与上面的问题不同,因为我想在 JSON ONLY 中而不是在 字符串的映射

中发布数据

【问题讨论】:

  • @Rgv 我已经给你代码了
  • 你想用空帖子发送数据
  • 我想以 JSON 格式发送数据,我将使用file_get_contents(php://input) 获取数据,问题是使用这种方式我获取x=abc&amp;y=def 格式的数据,我想要{"x":"abc", "y":"def"} 格式的JSON 数据

标签: android json android-studio android-volley jsonobject


【解决方案1】:

简单的方法是这样的

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);

}

【讨论】:

    【解决方案2】:

    试试这个:

    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 中也改变标题。

    【讨论】:

    • 是的!我也收到了 Stack Overflow 的问题。不过谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 2011-12-14
    • 2010-11-30
    • 1970-01-01
    • 2021-08-09
    相关资源
    最近更新 更多