Post request returns empty response body Android?

386 Views Asked by At

I'm using android-async-http for rest request. When I doing post request then the response body is empty. When I use postman for the same request I received a response as JSONObject.

 AsyncHttpClient client = new AsyncHttpClient();
            client.setBasicAuth(getResources().getString(R.string.api_user), getResources().getString(R.string.api_password));
            String requestAddress = getResources().getString(R.string.api_base_address) + getResources().getString(R.string.api_event_address);
            JSONObject params = new JSONObject();
            params.put("name", mEditTextName.getText().toString());
            params.put("place", mEditTextPlace.getText().toString());
            params.put("dateAndTime", DateUtils.sdfWithFullTime.format(DateUtils.sdfWithTime.parse(mEditTextDate.getText().toString())));
            Log.d(TAG, "onClick: " + params.toString());
            StringEntity stringParams = new StringEntity(params.toString());
            client.post(getApplicationContext(), requestAddress, stringParams, "application/json", new TextHttpResponseHandler() {
                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    Log.e(TAG, "onFailure: error during creating event " + responseString,throwable );
                    Toast.makeText(getBaseContext(),"Error during creating event",Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    Toast.makeText(getBaseContext(),"Successfully create event",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(getBaseContext(), EventListActivity.class);
                    startActivity(intent);
                }
            });
        } catch (Exception e) {
            Log.e(TAG, "createEvent: error during creating event", e);
        }

    }
1

There are 1 best solutions below

0
Ninad Hiray On

Check parameters and base url, use volley or retrofit library to Post request.