how to use asynx task to make api connection java

Solutions on MaxInterview for how to use asynx task to make api connection java by the best coders in the world

showing results for - "how to use asynx task to make api connection java"
Isaias
12 Jul 2019
1class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
2
3
4@Override
5protected void onPreExecute() {
6    super.onPreExecute();
7
8}
9
10@Override
11protected Boolean doInBackground(String... urls) {
12    try {
13
14        //------------------>>
15        HttpGet httppost = new HttpGet("YOU URLS TO JSON");
16        HttpClient httpclient = new DefaultHttpClient();
17        HttpResponse response = httpclient.execute(httppost);
18
19        // StatusLine stat = response.getStatusLine();
20        int status = response.getStatusLine().getStatusCode();
21
22        if (status == 200) {
23            HttpEntity entity = response.getEntity();
24            String data = EntityUtils.toString(entity);
25
26
27            JSONObject jsono = new JSONObject(data);
28
29            return true;
30        }
31
32
33    } catch (IOException e) {
34        e.printStackTrace();
35    } catch (JSONException e) {
36
37        e.printStackTrace();
38    }
39    return false;
40}
41
42protected void onPostExecute(Boolean result) {
43
44}
Nicolò
17 Aug 2017
1public class SomeOtherClass {   //Some url endpoint that you may have   String myUrl = "http://myApi.com/get_some_data";   //String to place our result in   String result;   //Instantiate new instance of our class   HttpGetRequest getRequest = new HttpGetRequest();   //Perform the doInBackground method, passing in our url   result = getRequest.execute(myUrl).get();}