如何在Android中使用Volley库将JSON对象发送到服务器?

时间:2022-10-22 20:23:22

I want to send a json object to the server using the post method.

我想使用post方法将json对象发送到服务器。

I have used volley library to pass the string params, and it's working fine, but when I run my app I am getting this:

我已经使用了齐射库来传递字符串参数,并且它工作正常,但是当我运行我的应用程序时,我得到了这个:

BasicNetwork.performRequest: Unexpected response code 400

BasicNetwork.performRequest:意外的响应代码400

my code:-

public class MainActivity extends AppCompatActivity {

    ProgressDialog pd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        makeJsonObjReq();
    }

    private void makeJsonObjReq() {

        JSONObject request=new JSONObject();
        try {
            request.put("ProductCode", "KK03672-038");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        pd = ProgressDialog.show(MainActivity.this, "Alert", "Please Wait...");
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(
                "URL",request,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {

                        pd.dismiss();
                        System.out.println("Response is====>" + response.toString());
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                pd.dismiss();
                System.out.println("Error is====>" + error.getMessage());
            }
        }) {

            /**
             * 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;
            }

            @Override
            public String getBodyContentType() {
                return "application/json";
            }
        };

        // Adding request to request queue:-
        AppController.getInstance().addToRequestQueue(jsonObjReq);
    }
}

AppContoller:-

public class AppController extends Application {

    public static final String TAG = AppController.class.getSimpleName();

    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;

    private static AppController mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized AppController getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    public ImageLoader getImageLoader() {
        getRequestQueue();
        if (mImageLoader == null) {
            mImageLoader = new ImageLoader(this.mRequestQueue,
                    new LruBitmapCache());
        }
        return this.mImageLoader;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        // set the default tag if tag is empty
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }
}

1 个解决方案

#1


2  

Try adding volley initialization code in Application class and reference this to your manifest application tag.

尝试在Application类中添加volley初始化代码,并将其引用到清单应用程序标记。

#1


2  

Try adding volley initialization code in Application class and reference this to your manifest application tag.

尝试在Application类中添加volley初始化代码,并将其引用到清单应用程序标记。