从Android发送JSON对象到HttpURLConnection POST请求

时间:2017-02-22 08:27:20

标签: android httpurlconnection

我试图将一个看起来像这样的json对象发送到我的服务器{' name':joe}, 目前我的代码看起来像这样。然而,我在我的服务器上的json看起来像{' {" name":joe}':''' }。所以我的问题是如何发送json,所以当我在服务器上收到它时,它看起来像{' name':joe}?我的服务器正在使用nodejs。我想通过request.body.name获取该名称。任何帮助将不胜感激

protected Boolean doInBackground(Void... params) {
        try {
            URL url = new URL("http://10.0.2.2:3000/users/user");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            String jsonString = "{\"name\":" + this.mName + "}";
            OutputStream os = conn.getOutputStream();
            os.write(jsonString.getBytes());
            os.flush();



            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (conn.getInputStream())));

            String output;
            StringBuilder response = new StringBuilder();
            while ((output = br.readLine()) != null) {
                response.append(output);
                response.append('\r');
            }
            mes = response.toString();
            conn.disconnect();

            if (mes!=null && !mes.isEmpty()){
                return true;
            }else {
                return false;
            }

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

        }
        return false;
    }

编辑:将application / x-www-form-urlencoded更改为application / json之后。我一直收到FileNorFoundException,服务器返回响应代码400

4 个答案:

答案 0 :(得分:0)

SanuoXeu在您的代码中更改此行将起作用

conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");

答案 1 :(得分:0)

试试这个:

 protected Boolean doInBackground(Void... params) {
            try {
                URL url = new URL("http://10.0.2.2:3000/users/user");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setDoOutput(true);
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");    

                DataOutputStream os = new DataOutputStream(conn.getOutputStream());

                JSONObject jsonParam = new JSONObject();
                jsonParam.put("name",this.mName);
                os.write(jsonParam.getBytes());
                os.flush();

                BufferedReader br = new BufferedReader(new InputStreamReader(
                        (conn.getInputStream())));

                String output;
                StringBuilder response = new StringBuilder();
                while ((output = br.readLine()) != null) {
                    response.append(output);
                    response.append('\r');
                }
                mes = response.toString();
                conn.disconnect();

                if (mes!=null && !mes.isEmpty()){
                    return true;
                }else {
                    return false;
                }

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

            }
            return false;
        }

答案 2 :(得分:0)

希望你做得很好。

如果您愿意通过连接字符串将数据作为JSON发送,那将会很难。

private String makeJson(String name) {

        JSONObject json = new JSONObject();
        try {
            json.put("name", name);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return json.toString();
    }

然后改变这一行:

conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");

答案 3 :(得分:0)

可能会有所帮助......

protected Boolean doInBackground(Void... params) {
        try {
            URL url = new URL("http://10.0.2.2:3000/users/user");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

         /***************** add this code **********************/
            JSONObject ap = new JSONObject();
            ap.put("name","Joe");

            OutputStreamWriter ap_osw= new OutputStreamWriter(conn.getOutputStream());
            ap_osw.write(ap.toString());
            ap_osw.flush();
            ap_osw.close();

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (conn.getInputStream())));

            String output;
            StringBuilder response = new StringBuilder();
            while ((output = br.readLine()) != null) {
                response.append(output);
                response.append('\r');
            }
            mes = response.toString();
            conn.disconnect();

            if (mes!=null && !mes.isEmpty()){
                return true;
            }else {
                return false;
            }

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

        }
        return false;
    }
相关问题