【问题标题】:Send JSON as a POST request to server by AsyncHttpClient通过 AsyncHttpClient 将 JSON 作为 POST 请求发送到服务器
【发布时间】:2018-01-12 23:36:24
【问题描述】:

我想使用 LoopJ 的 AsndroidAsyncHttpt 将 JSON 作为 POST 发送到我的本地主机服务器。我正在使用这种方法: public void post(Context context, String url, HttpEntity entity, String contentType, AsyncHttpResponseHandler responseHandler) 在我的代码中,但它不起作用。这是我的代码:

private void  loginUser() throws JSONException, UnsupportedEncodingException {
    String login = textLogin.getText().toString();
    String password = textPassword.getText().toString();

    JSONObject jsonObject = new JSONObject();
    if(Utility.isNotNull(login) && Utility.isNotNull(password)) {
        jsonObject.put("username", login);
        jsonObject.put("password", password);
        invokeWS(jsonObject);
    }
    else{
        Toast.makeText(getApplicationContext(), "Proszę wypełnić wszystkie pola!", Toast.LENGTH_LONG).show();
    }
}

private void invokeWS(JSONObject jsonObject) throws UnsupportedEncodingException {
    StringEntity entity = new StringEntity(jsonObject.toString());
    AsyncHttpClient client = new AsyncHttpClient();

    Log.i("SER", "http://" + Constants.address + ":" + Constants.port + "/silownia_java/rest/login/auth" + entity);
    Log.i("SER", "http://" + Constants.address + ":" + Constants.port + "/silownia_java/rest/login/auth" + jsonObject);

    client.post(getApplicationContext(), "http://" + Constants.address + ":" + Constants.port + "/silownia_java/rest/login/auth", entity, "application/json", new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject obj) {
            try {
                Log.i("SER", "HERE!");
                String login = obj.getString("login");
                int ID = obj.getInt("id");

                //user.setUserId(obj.getInt("userid"));
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
            if (statusCode == 404) {
                Toast.makeText(getApplicationContext(), "404 - Nie odnaleziono serwera!", Toast.LENGTH_LONG).show();
            } else if (statusCode == 500) {
                Toast.makeText(getApplicationContext(), "500 - Coś poszło nie tak po stronie serwera!", Toast.LENGTH_LONG).show();
            } else if (statusCode == 403) {
                Toast.makeText(getApplicationContext(), "Podano niepoprawne dane!", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(), throwable.toString(), Toast.LENGTH_LONG).show();
            }
        }
    });
}

我的Logs 看起来不错:

http://MY_IP_ADDRESS:8080/silownia_java/rest/login/authorg.apache.http.entity.StringEntity@384d6a6d
http://MY_IP_ADDRESS:8080/silownia_java/rest/login/auth{"username":"barni","password":"12345"}

但我得到这样的错误:

org.apache.http.client.HttpResponseException: Unsupported Media Type

另外,我知道服务器没有收到任何请求。那么,可能是什么原因呢?

【问题讨论】:

  • 又一个localhost ...服务器在设备上吗?
  • 是的,当然,我已将其更改为 localhost,仅用于stackoverflow,因为我不想显示我的 IP 地址。我知道服务器会响应,因为 get 请求有效。
  • 示例响应:{"login":"barni","id":"1"} 但据我所知,它是用字符串而不是 JSON 构建的,这可能是个问题?
  • 在服务器端,响应是这样构建的:return Response.status(200).entity("{\"login\":\""+user.getLogin()+"\",\"id\":\""+user.getUserId()+"\"}").build() 这可能是个问题?
  • 不,因为:另外,我知道服务器没有收到任何请求 ...问题是本地主机,只是 google for localhost+android(+emulator)

标签: android json android-async-http


【解决方案1】:

我通过将标题信息添加到我的 entity 对象来解决它。

ByteArrayEntity entity = new ByteArrayEntity(jsonObject.toString().getBytes("UTF-8"));
entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-31
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多