Horje
how to use asynx task to make api connection java Code Example
how to use asynx task to make api connection java
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {


@Override
protected void onPreExecute() {
    super.onPreExecute();

}

@Override
protected Boolean doInBackground(String... urls) {
    try {

        //------------------>>
        HttpGet httppost = new HttpGet("YOU URLS TO JSON");
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(httppost);

        // StatusLine stat = response.getStatusLine();
        int status = response.getStatusLine().getStatusCode();

        if (status == 200) {
            HttpEntity entity = response.getEntity();
            String data = EntityUtils.toString(entity);


            JSONObject jsono = new JSONObject(data);

            return true;
        }


    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {

        e.printStackTrace();
    }
    return false;
}

protected void onPostExecute(Boolean result) {

}
how to use asynctask to make api connection
public 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();}
Source: medium.com




Java

Related
condensed for loop java Code Example condensed for loop java Code Example
Java 8 merge multiple collections using flatmap Code Example Java 8 merge multiple collections using flatmap Code Example
max head java Code Example max head java Code Example
balanced string in java Code Example balanced string in java Code Example
show menuitem when menu hovered javafx Code Example show menuitem when menu hovered javafx Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
6