What is the best way to make an HTTP request on Android?

Asked

Viewed 1,573 times

1

I’m using DefaultHttpClient to make json requests for a Webservice, but this function is obsolete, which is the best alternative for creating a Webservice client?

  • 2

    Take a look in this question, even in the comments the ramaral answers that same question and indicates the alternative.

1 answer

2

Utlize Httpclient. See the example below:

package com.hostingcompass.web.controller;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;

public class WebCrawler {

    public static void main(String[] args) throws Exception {

        HttpClient client = HttpClientBuilder.create().build();
        HttpGet request = new HttpGet("http://mkyong.com");
        HttpResponse response = client.execute(request);
        //...

    }

}

No need to close the connection.

  • If the answer is correct, please do a "tick" on the green arrow in the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.