0

I'm new to Android. In this method, I'm willing to send a BEAccident object along with some Strings, but the server throws an error.

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

    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) {
        }
    }
}

Please advise if this is the right way to send a non-native object using URLConnection. I appreciate any response.

3
  • can you show the code on the server side? Commented Oct 16, 2014 at 21:58
  • @NagyVilmos I'm afraid it is not available. All I know is that it works perfectly in iOS. Thanks. Commented Oct 16, 2014 at 23:25
  • try to replace your ` data +="&" + URLEncoder.encode("Accident", "UTF-8") + "="+URLEncoder.encode(jsonAccident.toString(), "UTF-8");` this line with ` data +="&" + URLEncoder.encode("Accident", "UTF-8") + "="+jsonAccident.toString();` Commented Oct 17, 2014 at 4:28

2 Answers 2

3

This can occur if parameter names in your query string do not match with the web service's parameter names. it would be helpful if you can state what kind of server you are receiving.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, that was my first guess. (Un)fortunately the names are good.
1

I managed to make it work with the following approach:

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();
    }


}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.