json 对象的 Volley Post 方法 [英] Volley Post method for json object

查看:22
本文介绍了json 对象的 Volley Post 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

data={
    "request": {
       "type": "event_and_offer",
       "devicetype": "A"
    },
    "requestinfo": {
       "value": "offer"
      }
}

如何从 volley plz help 发布此请求

how to post this request from volley plz help

            JsonObjectRequest jsonObjReq = new JsonObjectRequest(
            Request.Method.POST,url, null   ,
            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/x-www-form-urlencoded");
            return headers;
        }

js 是对我的 jsson 对象的引用......我让我的 jsson 像这样......

js is reffer to my jsson object ...... i make my jsson like this.....

JSONObject jsonobject_one = new JSONObject();
    try {
        jsonobject_one.put("type", "event_and_offer");
        jsonobject_one.put("devicetype", "I");

        JSONObject jsonobject_TWO = new JSONObject();
        jsonobject_TWO.put("value", "event");
        jsonobject = new JSONObject();

        jsonobject.put("requestinfo", jsonobject_TWO);
        jsonobject.put("request", jsonobject_one);

        js = new JSONObject();
        js.put("data", jsonobject.toString());
        Log.e("created jsson", "" + js);

但是它没有响应值怎么办请帮忙

But it Not response the value what to do plzz help

推荐答案

首先你的json数据:

first your json data:

JSONObject js = new JSONObject();
try {
    JSONObject jsonobject_one = new JSONObject();

    jsonobject_one.put("type", "event_and_offer");
    jsonobject_one.put("devicetype", "I");

    JSONObject jsonobject_TWO = new JSONObject();
    jsonobject_TWO.put("value", "event");
    JSONObject jsonobject = new JSONObject();

    jsonobject.put("requestinfo", jsonobject_TWO);
    jsonobject.put("request", jsonobject_one);


    js.put("data", jsonobject.toString());

}catch (JSONException e) {
        e.printStackTrace();
}

然后是您的 json 请求:

then your json request:

JsonObjectRequest jsonObjReq = new JsonObjectRequest(
        Request.Method.POST,url, js,
        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;
    }

注意标题

如果您想在 localhost 中进行测试,请使用以下代码并设置您的 url 以连接您的 localhost 服务器和 IP 地址:下面的代码把你的所有请求都放在一个文本文件中,我试过了,它有效

and if you want to test in localhost use below code and set your url to connect your localhost server and ip address: below code put all of your request in a text file, i tried it and it works

<?php
file_put_contents('test.txt', file_get_contents('php://input'));
?>

这篇关于json 对象的 Volley Post 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆