【问题标题】:My JSONObject sending null values to server我的 JSONObject 向服务器发送空值
【发布时间】:2017-12-18 11:12:35
【问题描述】:

我正在尝试将文本值发送到服务器,这些值是名称、电话、位置、备注,但每次都缺少每个值 第一次名字不去,下次电话和下一个位置...

更新:

Jsoncode:

public static String POST(String url, Persoenter code heren person){
    InputStream inputStream = null;
    String result = "";
    try {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        String json = "";
        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulate("name",person.getName());
        jsonObject.accumulate("phone", person.getPhone());
        jsonObject.accumulate("remarrks",person.getRemarks());
        jsonObject.accumulate("credated_dt", person.getCreatedat());

        jsonObject.accumulate("emp_code", person.getCreatedby());

        jsonObject.accumulate("location", person.getLocation());
        jsonObject.accumulate("add_fld1", "Test");
        jsonObject.accumulate("add_fld2", "Test");
        jsonObject.accumulate("add_fld3", "Test");




        json = jsonObject.toString();
        StringEntity se = new StringEntity(json);
        Log.e("sent",""+json);
        httpPost.setEntity(se);
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

        HttpResponse httpResponse = httpclient.execute(httpPost);

        inputStream = httpResponse.getEntity().getContent();
        if(inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";
    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }
    return result;
}

Httppost

private class sync extends AsyncTask<String, String, String> {


    InputStream inputStream = null;
    OutputStream outputStream = null;

    protected String doInBackground(String... urls) {

        person = new Person();

        person.setName(eName.getText().toString());
        person.setPhone(ePhonenumber.getText().toString());
        person.setLocation(eLocation.getText().toString());
        person.setRemarks(eRemarks.getText().toString());
        person.setCreatedby(eCreatedby.getText().toString());
        person.setCreatedat(eCreatedat.getText().toString());
        return POST(urls[0],person);
    }
@Override
    protected void onPreExecute() {
        progress.setTitle("Connecting to server");
        progress.setMessage("Sending Data.... Please wait!");
        progress.setCanceledOnTouchOutside(false);
        progress.show();
    }

    @Override
    protected void onPostExecute(String s) {
                progress.dismiss();
                 Intent Homeintent = getIntent();
Homeintent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
startActivity(Homeintent);

    }

输出 1:{"name":"beta33","phone":"33","re​​marks":"33","credated_dt":"2017-12-20 11:56:32","emp_code":"test @gmail.com","位置":"33"} 2:{"name":"beta33","phone":"33","re​​marks":"33","credated_dt":"2017-12-20 11:56:43","emp_code":"test @gmail.com","位置":"33"} 3:{"name":"","phone":"34","re​​marks":"34","credated_dt":"2017-12-20 11:56:52","emp_code":"test@ gmail.com","位置":"34"}

在尝试一两次之后,现在名称不正确,我不知道为什么..请有人帮我解决这个问题

【问题讨论】:

  • 我建议你使用 volley 或 retrofit 之类的网络库来处理这类操作。这种程序已经过时,随着项目的扩展,它变得难以维护。
  • 是的,我想使用 volley..但到目前为止这是必需的
  • 在调试模式下检查JSONObject joj = getonline();的值。是否获得预期值。
  • 我建议通过 url 传递值。参考stackoverflow.com/a/8655039/5193533“数据”作为url字符串的一部分传递。
  • @KBJ 实际上我的 URL 连接只是一个有效的连接。它正在为其他选项正确发送值。我得到的错误是当网络在线时,如果我点击保存值编辑字段应该转到服务器,第一次所有值都成功,然后一些值变为空

标签: java android json httprequest


【解决方案1】:

我发布了一个工作代码示例,用这个替换你的,然后在这里找出错误所在。

try {
            URL url = new URL("http://-------");
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setRequestMethod("POST"); 
            httpURLConnection.setRequestProperty("Content-Type", "application/json"); 
            httpURLConnection.connect();
            DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
            wr.write(inputJson.getBytes());   //your input json in string format
            wr.flush();
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (httpURLConnection.getInputStream())));
            StringBuffer bfr = new StringBuffer();
            String output = "";
            while ((output = br.readLine()) != null) {
                bfr.append(output);
            }
            String serverPostRes = bfr.toString();
            wr.close();
            httpURLConnection.disconnect();
            bfr.delete(0,bfr.length());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return serverPostRes;
    }

你问题中的inputJson你可以通过

    String inputJson = "{   "
            + "     \"add_fld1\": \"" + ("Test"+ i) + "\","
            + "     \"add_fld2\": \"" + ("Test"+ i+1) + "\","
            + "     \"add_fld3\": \"" + ("Test"+ i+2) + "\","
            + "     \"credated_dt\": \"" + formattedDate + "\","
            + "     \"emp_code\": \"" + eCreatedby.getText().toString() + "\","
            + "     \"location\": \"" + eLocation.getText().toString() + "\","
            + "     \"name\": \"" + eName.getText().toString() + "\","
            + "     \"phone\": \"" + ePhonenumber.getText().toString() + "\","
            + "     \"remarrks\": \"" + eRemarks.getText().toString() + "\","
            + "}";

【讨论】:

  • 问题不在于连接..您的代码和我的代码之间的唯一区别是您使用的是 http post 而我使用的是 URL 连接..我的 URL 连接只是一个工作代码
  • 两个代码都使用“POST”方法。这个答案不包含将值传递到服务器端,我认为这是这里的主要问题。
  • 我也在使用相同的 'HttpURLConnection `。请再检查一次。问题在于发送请求和处理响应。
  • 左边有注释部分的行,这里inputJsonjson-string格式的输入值。 @KBJ @flash
  • 这段代码运行良好。 serverPostRes 是转换成字符串的响应。
猜你喜欢
  • 2016-10-06
  • 2017-05-04
  • 1970-01-01
  • 2012-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-31
  • 1970-01-01
相关资源
最近更新 更多