0

I have been trying to send JSON data to the server. However, my efforts have failed since NameValuePair is deprecated. And okHttp is giving some error like okie.string not found on library classpath. Does anyone know about it?. Please suggest me some other techniques. I tried a lot of Google however, the articles did not help since the articles were too old.

Any links or examples will definitely help.

Nevermore
  • 882
  • 2
  • 18
  • 44

2 Answers2

1

Use retrofit for this. Retrofit Update

GensaGames
  • 5,538
  • 4
  • 24
  • 53
1

ok, here is what I did, i will write my own HttpRequest library, not default one. You have to do many changes if you want to use this code, but to guide i think is good, later i write the code better.

First thing you have to jsonify your data on android in this way:

private JSONObject jsonifyData() throws JSONException {
        JSONArray jsonArray = new JSONArray();

        // first we begin with array form
        jsonArray.put(location.getLongitude());
        jsonArray.put(location.getLatitude());

        JSONObject mainObject = new JSONObject();
        mainObject.put(LOC,jsonArray);

        // here we put the rest of the data
        for ( String loc_key : location.getKeys())
            mainObject.put(loc_key, location.getParam(loc_key));

        for ( String key : ApplicationData.commercial_keys) {
            if ( !key.equalsIgnoreCase(ApplicationData.SERIAL) && comercial.containsKey(key))
                mainObject.put(key, comercial.get(key));
        }

        mainObject.put(API_DEVICE, t_device);
        mainObject.put(API_SCOPE, t_scope);
        mainObject.put(API_OS, API_ANDROID);

        String ip = "0.0.0.0";

        mainObject.put(API_IP, ip); // IP de salida del sitio

        return mainObject;
    }

I created this method for that job. I think there is no dificult to to this job, due time a put my own conversions, but a bit modify put yours.

Then, when you have a json format, have to put headers and send data:

try {
                JSONObject data = jsonifyData();
                Log.d(TAG, data.toString());

                String[] HEAD = {"Content-Type"};

                HashMap<String, String> cabeceras = new HashMap<>();
                cabeceras.put("Content-Type", "application/json");

                // you can ignore much of this
                HttpRequest register = new HttpRequest(
                        'your_url',
                        null
                );
                register.setMethod("POST", 1);
                register.setHeaders(HEAD, cabeceras);
                register.setQuery(data.toString());

                register.start();
                register.join();

                upload_data = true;

            } catch (JSONException | MalformedURLException | InterruptedException e) {
                e.printStackTrace();
            }
}

If you see, I use HttpRequest, it isn't the default one!! I created one with the same name, this is the code of the POST:

private JSONObject POST() throws JSONException {
    JSONObject response = null;
    BufferedReader bufferedReader = null;
    InputStreamReader in = null;
    HttpURLConnection conn = null;

    try {

        conn = (HttpURLConnection) this.url.openConnection();
        conn.setConnectTimeout(10000);
        conn.setReadTimeout(10000);
        conn.setDoOutput(true);
        conn.setRequestMethod(POST);

        for (String KEY : KEYS) conn.setRequestProperty(KEY, headers.get(KEY));

        conn.connect();

        PrintWriter out = new PrintWriter(conn.getOutputStream());
        out.print(postParameters);
        Log.d("HttpRequest--->","DATA TO SEND: "+postParameters);
        out.close();

        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            in = new InputStreamReader(conn.getInputStream());
            bufferedReader = new BufferedReader(in);
            String line;
            StringBuilder buffer = new StringBuilder();
            while ((line = bufferedReader.readLine()) != null) {
                buffer.append(line);
                buffer.append('\r');
            }

            bufferedReader.close();

            in.close();
            response = new JSONObject(buffer.toString());
        }

        conn.disconnect();

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (conn != null) {
                conn.disconnect();
            }
            if (bufferedReader != null) {
                bufferedReader.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return response;
}
Josete Manu
  • 187
  • 2
  • 16