我的JSONObject将空值发送到服务器

时间:2017-12-18 11:12:36

标签: java android json httprequest

我正在尝试向服务器发送文本值,包括名称,电话,位置,备注,但每次缺少每个值 第一次打电话和下一个位置......

更新

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:{&#34;名称&#34;:&#34; beta33&#34;&#34;电话&#34;:&#34; 33&#34;&#34; remarrks&#34;:&# 34; 33&#34;,&#34; credated_dt&#34;:&#34; 2017-12-20 11:56:32&#34;,&#34; emp_code&#34;:&#34; test @ gmail .COM&#34;&#34;位置&#34;:&#34; 33&#34;} 2:{&#34;名称&#34;:&#34; beta33&#34;&#34;电话&#34;:&#34; 33&#34;&#34; remarrks&#34;:&# 34; 33&#34;,&#34; credated_dt&#34;:&#34; 2017-12-20 11:56:43&#34;,&#34; emp_code&#34;:&#34; test @ gmail .COM&#34;&#34;位置&#34;:&#34; 33&#34;} 3:{&#34;名称&#34;:&#34;&#34;&#34;电话&#34;:&#34; 34&#34;&#34; remarrks&#34;:&# 34; 34&#34;,&#34; credated_dt&#34;:&#34; 2017-12-20 11:56:52&#34;,&#34; emp_code&#34;:&#34; test @ gmail .COM&#34;&#34;位置&#34;:&#34; 34&#34;}

在尝试了一两次尝试之后,这个名字不会发生,我不知道为什么......请有人帮我解决这个问题

1 个答案:

答案 0 :(得分:-2)

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

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() + "\","
            + "}";
相关问题