0

I have write a simple method to post data to URL and consume the output. I hava tried multiple ways to consume the out put but no success yet:

public void postToUrl(final String theurl, final String query, final Callable<Void> myMethod){
        String urlData="";
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String urlData="";
                    String url=baseurl+ theurl + "/?" +query;
                    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                    StrictMode.setThreadPolicy(policy);
                    URLConnection connection = new URL(url).openConnection();
                    connection.setDoOutput(true);
                    connection.connect();

                    InputStream response = connection.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(response));

                    String line="";
                    while ((line = reader.readLine()) != null) {
                        urlData += line;
                    }
                    reader.close();

                    // How to consume urlData here?
                    // myMethod.call();  not accepts parameter

                    try {
                        myMethod.call(urlData);
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }}
        ).start();
    }
  1. I have to wrap the method inside a runnable and can not expect a runnable method to return a value.
  2. I tried to consume the output when it is ready inside the runnable but I need to call a third party method to pass the output. I found callable but it does not accept parameters.
  3. I have read the accepted answer here but it needs to define a new class for every method.
  4. I have read this Q/A but it suggest to define an interface instead of method that I believe is not the proper use of interface.

If this method is not the proper way to call and consume a url, how do you manage multiple client-server request in an application? Do you rewrite the codes above for every type of request? Do you really define a new class or new interface for every clien-server interactions?

Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82
  • What is wrong with using an `AsyncTask` and consuming it in `onPostExecute`? – HB. Nov 23 '19 at 13:32
  • `StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);` Shame o shame. – blackapps Nov 23 '19 at 13:32
  • Try using third party libraries like retrofit. – Mahdi-Malv Nov 23 '19 at 13:44
  • I have same confusion in logic that how to assign the `onPostExecute` to different methods? Do you write a new class with its own `onPostExecute` for every request to a new url with different purpose?@HB. – Ali Sheikhpour Nov 23 '19 at 13:48
  • You should use `Retrofit` library to handle http requests, and avoid using `Thread` class in Android – Pavel Poley Nov 23 '19 at 15:11

0 Answers0