Query error for a JSON URL "getContentHandler, getContent"

Asked

Viewed 64 times

-1

I am starting the development of an app that performs queries to various websites with JSON return and treat this information in several ways.

To test the Urls I use the code below, works well with a third of the sites. I test them by this code and then collect the information I need in the app I’m developing, only many websites are presenting the error that I will highlight below and I’m not finding material on the internet to solve the problem.

Below I will put the code that works with several sites, but I will leave in it one of the sites that shows the error. If possible, in addition to indicating the material, send me the corrected code that I will analyze then, only I can not stop the APP now to analyze first, follow my code for testing:

package Metodos;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class Braziliex
{
    public static void main(String[] args) 
    {
        try {
              String sURL = "https://braziliex.com/api/v1/public/ticker/eth_brl";
                URL url = new URL(sURL);
                HttpURLConnection request = (HttpURLConnection) url.openConnection();
                request.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
                request.connect();
                JsonParser jp = new JsonParser(); 
                JsonObject root = (JsonObject) jp.parse(new InputStreamReader((InputStream) request.getContent()));
                System.out.println(root.toString());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

In the test I only try to make the impression on the console, if I succeed, I put the site inside my APP and treat the information as I need, but this site is giving the following error:

java.net.Unknownserviceexception: no content-type at java.net.Urlconnection.getContentHandler(Unknown Source) at java.net.Urlconnection.getContent(Unknown Source) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getContent(Unknown Source) At metodos.Braziliex.main(Returnvanegandolista.java:22)

Remembering that the site is active and working in the browser normally, bringing the information of the query. I’ve tried several ways to get the information and I never get out of this mistake, which I can do?

An example of site that works well with this code: https://api.bitcointrade.com.br/v2/public/BRLBTC/ticker

  • Maybe you are not sending all the necessary headers in Java. The browser sends them and can therefore receive the answer correctly. Start by trying it out here: request.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");. If it doesn’t work, I suggest clicking F12 in the browser, making the request, observing the headers that are sent and playing them in your code.

  • I think we’re on the right track, error 403 fixed, but we have another one now... java.net.Unknownserviceexception: no content-type at java.net.Urlconnection.getContentHandler(Unknown Source) at java.net.Urlconnection.getContent(Unknown Source) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getContent(Unknown Source) Methods.RetransformingLista.main(Returneset.java:22) ..... it would be interesting for me to update the question with the new error?

  • I added the code after modification and new error, thanks in advance for the help, I think we will be able to solve ... hugs.

1 answer

0


I was able to solve it, the error was now in the request.getContent command, I switched to request.getInputStream() and I was able to capture the value I wanted, thank you for starting the Statelessdev solution...

Browser other questions tagged

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