简体   繁体   中英

Parse simple JSON in Android 4.4 from URL

I am trying to parse very simple JSON in Android 4.4 from an URL that looks like this:

{
    "code":"asdfg"
}

I have a working code for Android 7.0, but for some reason it doesn't work in Android 4.4, and I have not have any luck with guides online.

Any help is appreciated :)

EDIT - current code (Works on Android 7 but not on Android 4.4):

static class Page {
String code;
}

public static String checkIp() throws Exception {
String json = readUrl("https://link.com/json.php");  //Not the real URL, replaced as an example
Gson gson = new Gson();
Page page = gson.fromJson(json, Page.class);
Log.d("MainActivity", page.code);
return (page.code);
}

private static String readUrl(String urlString) throws Exception {
BufferedReader reader = null;
try {
    URL url = new URL(urlString);
    reader = new BufferedReader(new InputStreamReader(url.openStream()));
    StringBuffer buffer = new StringBuffer();
    int read;
    char[] chars = new char[1024];
    while ((read = reader.read(chars)) != -1)
        buffer.append(chars, 0, read);

    return buffer.toString();
} finally {
    if (reader != null)
        reader.close();
}
}

It gives me an error on these lines:

reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuffer buffer = new StringBuffer();

The error:

javax.net.ssl.SSLException: SSL handshake aborted: ssl=0xb8ca3c40: I/O error during system call, Connection reset by peer
W/System.err:     at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
W/System.err:     at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:405)
W/System.err:     at com.android.okhttp.Connection.upgradeToTls(Connection.java:146)
W/System.err:     at com.android.okhttp.Connection.connect(Connection.java:107)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
W/System.err:     at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:246)
W/System.err:     at java.net.URL.openStream(URL.java:470)

Firstly, check the url is it working correctly. Sometimes it happens that link is not working. and check properly for HTTPS or HTTP because calling for https of http link(vise-versa) may rise this error. because of the handshake problem arise mostly.

calling of method "checkIp()" need to be from background thread as.

   new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    String s = checkIp();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

There is nothing wrong in code just except "thread" problem.

I think this may solve your problem. I check at my end it working for normal link.

Let me know if not working else.

The destination host doesn't have a proper signed certificate. Either add the host certificate to your device or get a proper certificate from issuing authority like GoDaddy or switch to http for testing purpose

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM