Send Gson (JSON) using HttpURLConnection

1

I am trying to send the data in JSON from android (client) to a PHP server that will only save this data in the database and send a 1 or a 0 depending on the success or failure of the INSERT operation. .

I read that there is a library called Gson to parse java objects to JSON. I searched for a code about this library and found that the way to walk the object would be like this: (MAL)

*Gson gson = new Gson();
gson.toJson(bollo, Pan.class); /*bollo es un objeto ya instaciado de la clase Pan*/*

The parson to JSON is wrong, according to link would be: (WELL)

Gson gson = new Gson();
String json = gson.toJson(bollo);

But I'm not sure that that is entirely correct.

I take this opportunity to put my class Pan.java :

public class Pan {

    String precio;
    String cereal;
    String semilla;
    String nombre;
    String coccion;
}

I have also implemented the setter and getter methods of each of the variables but for the moment I omit them for not putting much code.

On the other hand I looked for an example of sending a POST request with HttpURLConnection , totally unknown to me because I used the apache liberty, obsolete since some versions, and I have learned this, but it is a code that I do not understand and that I see that it lacks where to put the String that contains the JSON and the url of my server.

 public static String makeRequest(String uri, String json) {
        HttpURLConnection urlConnection;
        String url;
        String data = json;
        String result = null;
        try
        {
            urlConnection = (HttpURLConnection) ((new URL(uri).openConnection()));
            urlConnection.setDoOutput(true);
            urlConnection.setRequestProperty("Content-Type", "application/json");
            urlConnection.setRequestProperty("Accept", "application/json");
            urlConnection.setRequestMethod("POST");
            urlConnection.connect();

            OutputStream outputStream = urlConnection.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            writer.write(data);
            writer.close();
            outputStream.close();

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
            String line = null; StringBuilder sb = new StringBuilder();
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line);
            }
            bufferedReader.close();
            result = sb.toString();
        }
        catch (UnsupportedEncodingException e) {
            e.printStackTrace(); }
        catch (IOException e) {
            e.printStackTrace(); }
        return result;
    }

My questions are:

  • Is the pan object well parsed to JSON by using Gson? It was wrong , I leave the fault
  • How do I send this string in JSON through HttpURLConnection?

Thank you very much, I know that the code is very green, but I can not find anything and I'm a little crazy.

Greetings.

    
asked by wiki 02.11.2017 в 20:48
source

0 answers