Skip to main content
added 6 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Android network apiAPI request

I often use a similar scheme work with apian API project. MaybeCan it can be made easier, or is there a standard approach?

This class who create apicreates an API request.:

public class ApiRequestCreator {

     private static String TEST_API_REQUEST = "http://jsonplaceholder.typicode.com/posts";

     public static Request createTestApiRequest(){
         return new Request.Builder()
                .url(TEST_API_REQUEST)
                .build();
     }
}

This class execute the request:

 public class ApiRequestExecutor {

     private final OkHttpClient client = new OkHttpClient();

     public void run(final Request request, final ApiMethodListener apiMethodListener) {

         new AsyncTask<Void, Void, String>() {

                @Override
                protected String doInBackground(Void ...voids) {
                try {
                     Response response = client.newCall(request).execute();
                     if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
                     return response.body().string();
                } catch (IOException e) {
                     e.printStackTrace();
                     return null;
                }
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                apiMethodListener.result(result);
            }

        }.execute();

    }
}

For all of this to start:

new ApiRequestExecutor().run(ApiRequestCreator.createTestApiRequest(), call_back_interface_here);

Android network api request

I often use a similar scheme work with api project. Maybe it can be made easier, or is there a standard approach?

This class who create api request.

public class ApiRequestCreator {

     private static String TEST_API_REQUEST = "http://jsonplaceholder.typicode.com/posts";

     public static Request createTestApiRequest(){
         return new Request.Builder()
                .url(TEST_API_REQUEST)
                .build();
     }
}

This class execute request

 public class ApiRequestExecutor {

     private final OkHttpClient client = new OkHttpClient();

     public void run(final Request request, final ApiMethodListener apiMethodListener) {

         new AsyncTask<Void, Void, String>() {

                @Override
                protected String doInBackground(Void ...voids) {
                try {
                     Response response = client.newCall(request).execute();
                     if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
                     return response.body().string();
                } catch (IOException e) {
                     e.printStackTrace();
                     return null;
                }
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                apiMethodListener.result(result);
            }

        }.execute();

    }
}

For all this to start

new ApiRequestExecutor().run(ApiRequestCreator.createTestApiRequest(), call_back_interface_here);

Android network API request

I often use a similar scheme work with an API project. Can it be made easier, or is there a standard approach?

This class creates an API request:

public class ApiRequestCreator {

     private static String TEST_API_REQUEST = "http://jsonplaceholder.typicode.com/posts";

     public static Request createTestApiRequest(){
         return new Request.Builder()
                .url(TEST_API_REQUEST)
                .build();
     }
}

This class execute the request:

 public class ApiRequestExecutor {

     private final OkHttpClient client = new OkHttpClient();

     public void run(final Request request, final ApiMethodListener apiMethodListener) {

         new AsyncTask<Void, Void, String>() {

                @Override
                protected String doInBackground(Void ...voids) {
                try {
                     Response response = client.newCall(request).execute();
                     if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
                     return response.body().string();
                } catch (IOException e) {
                     e.printStackTrace();
                     return null;
                }
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                apiMethodListener.result(result);
            }

        }.execute();

    }
}

For all of this to start:

new ApiRequestExecutor().run(ApiRequestCreator.createTestApiRequest(), call_back_interface_here);
edited tags; edited tags
Link
200_success
  • 145.6k
  • 22
  • 191
  • 481
Source Link

Android network api request

I often use a similar scheme work with api project. Maybe it can be made easier, or is there a standard approach?

This class who create api request.

public class ApiRequestCreator {

     private static String TEST_API_REQUEST = "http://jsonplaceholder.typicode.com/posts";

     public static Request createTestApiRequest(){
         return new Request.Builder()
                .url(TEST_API_REQUEST)
                .build();
     }
}

This class execute request

 public class ApiRequestExecutor {

     private final OkHttpClient client = new OkHttpClient();

     public void run(final Request request, final ApiMethodListener apiMethodListener) {

         new AsyncTask<Void, Void, String>() {

                @Override
                protected String doInBackground(Void ...voids) {
                try {
                     Response response = client.newCall(request).execute();
                     if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
                     return response.body().string();
                } catch (IOException e) {
                     e.printStackTrace();
                     return null;
                }
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                apiMethodListener.result(result);
            }

        }.execute();

    }
}

For all this to start

new ApiRequestExecutor().run(ApiRequestCreator.createTestApiRequest(), call_back_interface_here);