0

I have realized that for config changes AsyncTaskLoader works better. But, does this request also works for POST method.

All the requests I have seen so far only show GET request. A sample code will help me understand how to send the parameters and make POST request using HttpUrlConnection and AsyncTaskLoader.

Sree
  • 1,694
  • 1
  • 18
  • 29
gorp88
  • 105
  • 14
  • Try this for post example using `AsyncTaskLoader` with `HttpUrlConnection `https://stackoverflow.com/a/29553137/3505534 – Ramesh sambu Dec 21 '17 at 15:32
  • I already have that in place, but I want to try using asynctaskloader. That for the comment though – gorp88 Dec 21 '17 at 15:34
  • https://stackoverflow.com/questions/29552946/easy-way-to-do-post-on-httpurlconnection/29553137#29553137, the above example is for AsyncTask and not AsyncTaskLoader. Although AsyncTask does the job @R2R – gorp88 Dec 21 '17 at 15:48

1 Answers1

0

Refer below link for AsyncTaskLoader. AsyncTaskLoader basic example. (Android)

You can use AsyncTask. Check below reference code.

private class LongOperation extends AsyncTask<String, Void, String> {

            @Override
            protected String doInBackground(String... params) {

URL url = new URL("http://url.com");
                HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
                conn.setReadTimeout(10000);
                conn.setConnectTimeout(15000);
                conn.setRequestMethod("POST");
                conn.setDoInput(true);
                conn.setDoOutput(true);

                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("param1", paramValue1));
                params.add(new BasicNameValuePair("param2", paramValue2));
                params.add(new BasicNameValuePair("param3", paramValue3));

                OutputStream os = conn.getOutputStream();
                BufferedWriter writer = new BufferedWriter(
        new OutputStreamWriter(os, "UTF-8"));
                writer.write(getQuery(params));
                writer.flush();
                writer.close();
                os.close();

                conn.connect();
            }

    private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
    {
        StringBuilder result = new StringBuilder();
        boolean first = true;

        for (NameValuePair pair : params)
        {
            if (first)
                first = false;
            else
                result.append("&");

            result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
        }

        return result.toString();
    }

            @Override
            protected void onPostExecute(String result) {
               if(!TextUtils.isEmpty(result)){
                   // Do your work
               }
            }

            @Override
            protected void onPreExecute() {}

            @Override
            protected void onProgressUpdate(Void... values) {}
        }