Author

Topic: Can't get API response (Read 180 times)

member
Activity: 69
Merit: 53
February 17, 2022, 03:03:51 AM
#5
@Sanka555
Possible issue related to output headers, api server don't understand your request so returning you 403.
Take a look at https://cloud.google.com/appengine/docs/standard/java/issue-requests#Setting_Request_Headers
legendary
Activity: 1568
Merit: 6660
bitcoincleanup.com / bitmixlist.org
February 16, 2022, 12:41:35 PM
#4
Side note: OP is trying to fetch balance from an Ethereum Classic address using a 3rd party API.


I would suggest not using StringBuilder() class and use String() directly because the StringBuilder might be adding extra characters inside your URL, and the server could be returning 403 for Not Found error (some hosts are configured for that instead of using 404).
member
Activity: 96
Merit: 36
February 13, 2022, 09:47:36 AM
#3
Maybe it would be better if you create a class which corresponds to result and then you convert result to your local class.
Or use more abstract approach, like casting to Map. See how I check balance using blockchain.com api:
https://github.com/PawelGorny/lostword/blob/master/src/main/java/com/pawelgorny/lostword/WorkerUnknownCheckAll.java


Code:
private static long checkBalance(List addresses) throws IOException, InterruptedException {
        String urlbalance = "https://blockchain.info/balance?active=";
        BufferedReader in;
        URL url = new URL(urlbalance + Joiner.on(",").join(addresses));
        in = new BufferedReader(new InputStreamReader(url.openStream()));
        String json="";
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            json += inputLine;
        }
        boolean x = false;
        Map addressBalance = GSON.fromJson(json, Map.class);
        long final_balance = 0L;
        for (Map.Entry entry:addressBalance.entrySet()){
            final_balance = ((Double)entry.getValue().get("final_balance")).longValue();
            if (final_balance>0){
                if (!x){
                    System.out.println("");
                }
                System.out.println(Utils.SPACE_JOINER.join(addresses)+" "+entry.getKey()+" "+final_balance);
                x = true;
            }
        }
        Thread.sleep(2000);
        return final_balance;
    }

Thank you. You have a great program. (I'm now looking for the missing word in your program Smiley ) But in this case, the problem is not in the output setting. The server gives 403. I don't even get to the output.


I think the problem is in the encoding or request settings(
legendary
Activity: 952
Merit: 1385
February 13, 2022, 07:22:42 AM
#2
Maybe it would be better if you create a class which corresponds to result and then you convert result to your local class.
Or use more abstract approach, like casting to Map. See how I check balance using blockchain.com api:
https://github.com/PawelGorny/lostword/blob/master/src/main/java/com/pawelgorny/lostword/WorkerUnknownCheckAll.java


Code:
private static long checkBalance(List addresses) throws IOException, InterruptedException {
        String urlbalance = "https://blockchain.info/balance?active=";
        BufferedReader in;
        URL url = new URL(urlbalance + Joiner.on(",").join(addresses));
        in = new BufferedReader(new InputStreamReader(url.openStream()));
        String json="";
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            json += inputLine;
        }
        boolean x = false;
        Map addressBalance = GSON.fromJson(json, Map.class);
        long final_balance = 0L;
        for (Map.Entry entry:addressBalance.entrySet()){
            final_balance = ((Double)entry.getValue().get("final_balance")).longValue();
            if (final_balance>0){
                if (!x){
                    System.out.println("");
                }
                System.out.println(Utils.SPACE_JOINER.join(addresses)+" "+entry.getKey()+" "+final_balance);
                x = true;
            }
        }
        Thread.sleep(2000);
        return final_balance;
    }
member
Activity: 96
Merit: 36
February 13, 2022, 12:27:34 AM
#1
Quote

org.json
json
20160810
I'm trying to send a simple request:

Quote
protected static void checkBalances() throws JSONException, IOException, InterruptedException {
        StringBuilder urlBuilder = new StringBuilder("https://blockscout.com/etc/mainnet/api?module=account&action=balance&address=0x37802776D2A1654C8888da63757e0Df4440DA79B");
                JSONObject results = readJsonFromUrl(urlBuilder.toString());
        System.out.println("*"+results+"*");
        System.exit(1);
       
    }
 
    public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
        InputStream is = new URL(url).openStream();
        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
            String jsonText = readAll(rd);
            return new JSONObject(jsonText);
        } finally {
            is.close();
        }
    }
 
    protected static String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
            sb.append((char) cp);
        }
        return sb.toString();
    }

flies in response
Quote
java.io.IOException: Server returned HTTP response code: 403 for URL: https://blockscout.com/etc/mai... f4440DA79B
at sun.net.http://www.protocol.http.HttpU... .java:1900)
at sun.net.http://www.protocol.http.HttpU... .java:1498)
at sun.net.http://www.protocol.https.Http... l.java:268)
at java.net.URL.openStream(URL.java:1067)
at Main.readJsonFromUrl(Main.java:352)
......


the same request in the browser gives
Quote
{"message":"OK","result":"840524398469120","status":"1"}
what am i doing wrong please tell me
Jump to: