如何通过URLConnection发送多个JSON对象?

时间:2014-10-16 20:52:36

标签: android json urlencode

我是Android新手。在这种方法中,我愿意发送BEAccident对象和一些字符串,但是服务器会抛出错误。

public String SendAccident(BEAccident accident,String username,             字符串密码){

    JSONObject jsonAccident = new JSONObject();
    try {
        jsonAccident.put("HasDocuments", accident.getHasDocuments());
        jsonAccident.put("ChassisNumber", accident.getChassisNumber());
        jsonAccident.put("ContainerNumber", accident.getContainerNumber());
        jsonAccident.put("Description", accident.getDescription());
        jsonAccident.put("IdTMUser", accident.getIdTMUser());
        jsonAccident.put("LicensePlate", accident.getLicensePlate());
        jsonAccident.put("LocalDateTime", accident.getLocalDateTime());
    } catch (JSONException e1) {
        return e1.getMessage();
    }

    try{
        // Set Request parameter
        data +="&" + URLEncoder.encode("Username", "UTF-8") + "="+username;
        data +="&" + URLEncoder.encode("Password", "UTF-8") + "="+password;
        data +="&" + URLEncoder.encode("LoadNumber", "UTF-8") + "="+"LN0003";
        data +="&" + URLEncoder.encode("Accident", "UTF-8") + "="+URLEncoder.encode(jsonAccident.toString(), "UTF-8");

    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    BufferedReader reader = null;
    // Send data
    try {

        URL url = new URL(URL_SEND_ACCIDENT);
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(
                conn.getOutputStream());

        wr.write(data);
        wr.flush();

        // Get the server response

        reader = new BufferedReader(new InputStreamReader(
                conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line = null;

        // Read Server Response
        while ((line = reader.readLine()) != null) {
            // Append server response in string
            sb.append(line + "");
        }

        // Append Server Response To Content String
        Content = sb.toString();
        return Content;
    } catch (Exception ex) {
        Error = ex.getMessage();
        return Error;
    } finally {
        try {

            reader.close();
        }

        catch (Exception ex) {
        }
    }
}

请告知这是否是使用URLConnection发送非本机对象的正确方法。我感谢任何回应。

2 个答案:

答案 0 :(得分:3)

如果查询字符串中的参数名称与Web服务的参数名称不匹配,则会发生这种情况。如果您可以说明您正在接收哪种服务器,将会很有帮助。

答案 1 :(得分:1)

我设法使用以下方法:

public String SendAccident(BEAccident accident, String username,
        String password) {

    JSONObject jsonAccident = new JSONObject();
    JSONObject parameter = new JSONObject();
    try {
        jsonAccident.put("HasDocuments", accident.getHasDocuments());
        jsonAccident.put("ChassisNumber", accident.getChassisNumber());
        jsonAccident.put("ContainerNumber", accident.getContainerNumber());
        jsonAccident.put("Description", accident.getDescription());
        jsonAccident.put("IdTMUser", 1);
        jsonAccident.put("LicensePlate", accident.getLicensePlate());
        jsonAccident.put("LocalDateTime", accident.getLocalDateTime());


        parameter.put("Username", username);
        parameter.put("Password", password);
        parameter.put("LoadNumber", accident.getLoadNumber());
        parameter.put("Accident", jsonAccident);
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        return e1.getMessage();
    }

    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
                                                                            // Limit
    HttpResponse response;

    try {
        HttpPost post = new HttpPost(URL_SEND_ACCIDENT);


        StringEntity se = new StringEntity(parameter.toString());

        post.setHeader("Content-type", "application/json");


        post.setEntity(se);


        response = client.execute(post);
        int status = response.getStatusLine().getStatusCode();
        BufferedReader reader = null;
        /* Checking response */
        if (response != null) {
            InputStream in = response.getEntity().getContent(); // Get the
                                                                // data in
                                                                // the
                                                                // entity
            reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder sb = new StringBuilder();
            String line = null;

            // Read Server Response
            while ((line = reader.readLine()) != null) {
                // Append server response in string
                sb.append(line + "");
            }

            // Append Server Response To Content String
            Content = sb.toString();
            return Content;

        }
        return "Response is null";

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


}