Search in sources :

Example 36 with JSONArray

use of run.wallet.common.json.JSONArray in project run-wallet-android by runplay.

the class JSONUrlReader method readJsonArrayFromUrl.

public static JSONArray readJsonArrayFromUrl(Context context, String url) {
    // Log.e("JSON",url);
    URL serverUrl = null;
    HttpURLConnection urlConnection = null;
    try {
        serverUrl = new URL(url);
        urlConnection = (HttpURLConnection) serverUrl.openConnection();
    } catch (Exception e) {
    // Log.e("JSON1", "" + e.getMessage());
    }
    JSONArray json = null;
    String useCookie = cookieStore.get(url);
    InputStream is = null;
    if (useCookie != null) {
        urlConnection.setRequestProperty("Cookie", useCookie);
    }
    try {
        urlConnection.setRequestProperty("User-Agent", getUserAgent(context));
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.setConnectTimeout(URL_TIMEOUT_MILLIS);
        urlConnection.setReadTimeout(URL_TIMEOUT_MILLIS);
        urlConnection.setInstanceFollowRedirects(true);
    } catch (Exception e) {
    // Log.e("readJsonObjectFromUrl().error.msg","2:"+e.getMessage());
    }
    try {
        // urlConnection.conn
        urlConnection.connect();
    } catch (Exception e) {
        cookieStore.remove(url);
    // Log.e("readJsonObjectFromUrl().error.msg","3 - connect(): "+e.getMessage());
    }
    try {
        is = urlConnection.getInputStream();
    } catch (IOException e) {
    // Log.e("JSON4",""+e.getMessage());
    }
    try {
        BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
        // BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String jsonText = readAll(rd);
        // Log.e("GOTJSON",jsonText+"");
        json = new JSONArray(jsonText);
    } catch (Exception e) {
    // Log.e("JSON5",""+e.getMessage());
    }
    try {
        is.close();
    } catch (Exception e) {
    // Log.e("JSON6",""+e.getMessage());
    }
    return json;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) JSONArray(run.wallet.common.json.JSONArray) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) URL(java.net.URL) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

JSONArray (run.wallet.common.json.JSONArray)36 SharedPreferences (android.content.SharedPreferences)33 JSONObject (run.wallet.common.json.JSONObject)21 ArrayList (java.util.ArrayList)6 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 WebGetExchangeRatesResponse (run.wallet.iota.api.responses.WebGetExchangeRatesResponse)1