Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP Transport and HTTP Request

I'm trying to fetch a JSON from an API using HTTP Request. However, I'm still unable to set up the request. Here is my current code

HttpTransport transport = new HttpTransport() {
    @Override
    protected LowLevelHttpRequest buildRequest(String s, String s1) throws IOException {
        return null;
    }
};

I was forced to create an HTTP Transport object, but I have no idea what to put in it, documentation doesn't give me any clues too

@Override
public void action(String[] args, MessageReceivedEvent event) {
    HttpRequest request=new HttpRequest(transport,"GET");
    request.setUrl(url);
    request.setConnectTimeout(5000);
    try {
        request.execute();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}

url is already containing the Json Web Token used for Authorisation.

However, when initializing the HttpRequest, it gives me the following error:

'HttpRequest(com.google.api.client.http.HttpTransport, java.lang.String)' is not public
in 'com.google.api.client.http.HttpRequest'.
Cannot be accessed from outside package

This error does even appear, when I clear the brackets:

HttpRequest request=new HttpRequest();

Does anyone know, what I'm doing wrong?

Update: I've fixed my code, but still get an error:

HttpTransport transport = new ApacheHttpTransport();
    HttpRequestFactory requestFactory = transport.createRequestFactory();
    try {
        HttpRequest request = requestFactory.buildGetRequest(url);
        request.execute();
        System.out.println(request.getContent());
    }
    catch (IOException e){
        e.printStackTrace();
    }

The error I get shown is:

avax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

Edit: Even if I use the example below, it returnes this error

like image 277
Werner der Champ Avatar asked Feb 13 '26 03:02

Werner der Champ


1 Answers

That means the HttpRequest constructor is protected to call. You have to use HttpRequestFactory factory to create a HttpRequest instance.

To get HttpRequestFactory you have to had HttpTransport see https://developers.google.com/api-client-library/java/google-http-java-client/reference/1.20.0/com/google/api/client/http/HttpTransport

HttpTransport transport = new ApacheHttpTransport();
HttpRequestFactory requestFactory = transport.createRequestFactory();
HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse response = request.execute();
     try {
       // process the HTTP response object
           InputStream is = response.getContent();
           try {
               // Process the input stream..
           } finally {
               is.close();
           }
     } finally {
       response.disconnect();
     }

Never tried the example hope it will work.

like image 69
Milan Baran Avatar answered Feb 14 '26 16:02

Milan Baran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!