发布请求返回的响应正文为Android空?

时间:2020-02-10 17:28:58

标签: android android-async-http

我正在使用android-async-http进行休息请求。当我执行发布请求时,响应主体为空。当我对同一请求使用邮递员时,收到的响应为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 个答案:

答案 0 :(得分:0)

检查参数和基本网址,使用排球或改型库发布请求。