List of usage examples for android.util JsonReader JsonReader
public JsonReader(Reader in)
From source file:org.liberty.android.fantastischmemo.downloader.quizlet.lib.java
public static String[] getAccessTokens(final String[] requests) throws Exception { final String TAG = "getAccesTokens"; String code = requests[0];//from ww w .ja v a 2s.com String clientIdAndSecret = QUIZLET_CLIENT_ID + ":" + QUIZLET_CLIENT_SECRET; String encodedClientIdAndSecret = Base64.encodeToString(clientIdAndSecret.getBytes(), 0); URL url1 = new URL("https://api.quizlet.com/oauth/token"); HttpsURLConnection conn = (HttpsURLConnection) url1.openConnection(); conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); // Add the Basic Authorization item conn.addRequestProperty("Authorization", "Basic " + encodedClientIdAndSecret); conn.setRequestMethod("POST"); conn.setDoInput(true); conn.setDoOutput(true); String payload = String.format("grant_type=%s&code=%s&redirect_uri=%s", URLEncoder.encode("authorization_code", "UTF-8"), URLEncoder.encode(code, "UTF-8"), URLEncoder.encode(Data.RedirectURI, "UTF-8")); OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); out.write(payload); out.close(); if (conn.getResponseCode() / 100 >= 3) { Log.e(TAG, "Http response code: " + conn.getResponseCode() + " response message: " + conn.getResponseMessage()); JsonReader r = new JsonReader(new InputStreamReader(conn.getErrorStream(), "UTF-8")); String error = ""; r.beginObject(); while (r.hasNext()) { error += r.nextName() + r.nextString() + "\r\n"; } r.endObject(); r.close(); Log.e(TAG, "Error response for: " + url1 + " is " + error); throw new IOException("Response code: " + conn.getResponseCode()); } JsonReader s = new JsonReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); try { String accessToken = null; String userId = null; s.beginObject(); while (s.hasNext()) { String name = s.nextName(); if (name.equals("access_token")) { accessToken = s.nextString(); } else if (name.equals("user_id")) { userId = s.nextString(); } else { s.skipValue(); } } s.endObject(); s.close(); return new String[] { accessToken, userId }; } catch (Exception e) { // Throw out JSON exception. it is unlikely to happen throw new RuntimeException(e); } finally { conn.disconnect(); } }
From source file:at.ac.tuwien.caa.docscan.logic.DataLog.java
private ArrayList<ShotLog> readJsonStream(InputStream in) throws IOException, ParseException { JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); ArrayList<ShotLog> shotLog = readList(reader); reader.close();/* ww w . j a v a2 s .c o m*/ return shotLog; }
From source file:com.thingsee.tracker.REST.KiiBucketRequestAsyncTask.java
private JSONArray readSensorDataFromString(String input, int offset) { StringReader reader = new StringReader(input); try {//from w w w. j a va 2 s .c o m reader.skip(offset); } catch (IOException e1) { e1.printStackTrace(); } JsonReader jsonReader = new JsonReader(reader); JSONArray jsonArray = new JSONArray(); try { jsonReader.beginArray(); while (jsonReader.hasNext()) { JSONObject jsonObject = readSingleData(jsonReader); jsonArray.put(jsonObject); } jsonReader.endArray(); } catch (IOException e) { // Ignore for brevity } catch (JSONException e) { // Ignore for brevity } try { jsonReader.close(); } catch (IOException e) { // Ignore for brevity } reader.close(); return jsonArray; }
From source file:pedromendes.tempodeespera.HospitalDetailActivity.java
public List<Emergency> readJsonStream(InputStream in) throws IOException { JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); try {/*from www . j a v a 2 s . c o m*/ return readHospitalDetailGetResponse(reader); } finally { reader.close(); } }
From source file:android.support.test.espresso.web.model.ModelCodec.java
private static Object decodeViaJSONReader(String json) throws IOException { JsonReader reader = null;// w w w . ja v a 2 s. c o m try { reader = new JsonReader(new StringReader(json)); while (true) { switch (reader.peek()) { case BEGIN_OBJECT: return decodeObject(reader); case BEGIN_ARRAY: return decodeArray(reader); default: throw new IllegalStateException("Bogus document: " + json); } } } finally { if (null != reader) { try { reader.close(); } catch (IOException ioe) { Log.i(TAG, "json reader - close exception", ioe); } } } }
From source file:com.fuzz.android.limelight.util.JSONTool.java
/** * Entry point reader method that starts the call of other reader methods. * * @param inputStream/*w ww . j ava2 s .c o m*/ * @return Book object after reading JSON is complete * @throws IOException */ public static Book readJSON(InputStream inputStream) throws IOException { JsonReader jsonReader = new JsonReader(new InputStreamReader(inputStream, "UTF-8")); try { return readBook(jsonReader); } catch (Throwable e) { throw new RuntimeException(e); } finally { jsonReader.close(); } }
From source file:com.avalond.ad_blocak.MainActivity.java
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d("MainActivity", "onActivityResult: Received result=" + resultCode + " for request=" + requestCode); super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_FILE_OPEN && resultCode == RESULT_OK) { Uri selectedfile = data.getData(); //The uri with the location of the file try {//from w ww . ja va 2s . c om Configuration newConfig = new Configuration(); newConfig.read( new JsonReader(new InputStreamReader(getContentResolver().openInputStream(selectedfile)))); config = newConfig; } catch (Exception e) { Toast.makeText(this, "Cannot read file: " + e.getMessage(), Toast.LENGTH_SHORT).show(); } reload(); FileHelper.writeSettings(this, MainActivity.config); } if (requestCode == REQUEST_FILE_STORE && resultCode == RESULT_OK) { Uri selectedfile = data.getData(); //The uri with the location of the file JsonWriter writer = null; try { writer = new JsonWriter( new OutputStreamWriter(getContentResolver().openOutputStream(selectedfile))); config.write(writer); writer.close(); } catch (Exception e) { Toast.makeText(this, "Cannot write file: " + e.getMessage(), Toast.LENGTH_SHORT).show(); } finally { try { writer.close(); } catch (Exception ignored) { } } reload(); } if (requestCode == REQUEST_ITEM_EDIT && resultCode == RESULT_OK) { Configuration.Item item = new Configuration.Item(); Log.d("FOOOO", "onActivityResult: item title = " + data.getStringExtra("ITEM_TITLE")); item.title = data.getStringExtra("ITEM_TITLE"); item.location = data.getStringExtra("ITEM_LOCATION"); item.state = data.getIntExtra("ITEM_STATE", 0); this.itemChangedListener.onItemChanged(item); } }
From source file:org.opensilk.music.ui2.loader.PluginLoader.java
public List<ComponentName> readDisabledPlugins() { List<ComponentName> list = new ArrayList<>(); String json = settings.getString(PREF_DISABLED_PLUGINS, null); Timber.v("Read disabled plugins=" + json); if (json != null) { JsonReader jr = new JsonReader(new StringReader(json)); try {//from w ww .ja va2 s. c o m jr.beginArray(); while (jr.hasNext()) { list.add(ComponentName.unflattenFromString(jr.nextString())); } jr.endArray(); } catch (IOException e) { settings.remove(PREF_DISABLED_PLUGINS); list.clear(); } finally { IOUtils.closeQuietly(jr); } } return list; }
From source file:org.liberty.android.fantastischmemo.downloader.quizlet.lib.java
private static String makePostApiCall(URL url, String content, String authToken) throws IOException { HttpsURLConnection conn = null; OutputStreamWriter writer = null; String res = ""; try {/*from w w w .jav a 2s.c o m*/ conn = (HttpsURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.addRequestProperty("Authorization", "Bearer " + authToken); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); writer = new OutputStreamWriter(conn.getOutputStream()); writer.write(content); writer.close(); if (conn.getResponseCode() / 100 >= 3) { Log.v("makePostApiCall", "Post content is: " + content); String error = ""; try { JsonReader r = new JsonReader(new InputStreamReader(conn.getErrorStream(), "UTF-8")); r.beginObject(); while (r.hasNext()) { error += r.nextName() + ": " + r.nextString() + "\r\n"; } r.endObject(); r.close(); } catch (Throwable eex) { } Log.v("makePostApiCall", "Error string is: " + error); res = error; throw new IOException( "Response code: " + conn.getResponseCode() + " URL is: " + url + " \nError: " + error); } else { JsonReader r = new JsonReader(new InputStreamReader(conn.getInputStream())); r.beginObject(); while (r.hasNext()) { try { res += r.nextName() + ": " + r.nextString() + "\n"; } catch (Exception ex) { r.skipValue(); } } return res; } } finally { conn.disconnect(); //return res; } }
From source file:com.dalaran.async.task.http.AbstractHTTPService.java
@SuppressLint("NewApi") public JsonReader callHttpToGetJsonReader(REQUEST request, String url, String host, int port) throws IOException { /* HttpHost targetHost = new HttpHost(host, port); HttpClient httpClient = new DefaultHttpClient(); HttpContext httpContext = new BasicHttpContext(); HttpGet httpGet = new HttpGet(url); setAuthentificationHeader(httpGet); /*from w w w. ja v a2 s . c o m*/ HttpResponse response = httpClient.execute(targetHost, httpGet, httpContext); HttpEntity entity = response.getEntity();*/ HttpEntity entity = makeRequest(request, url, host, port); return new JsonReader(new InputStreamReader(entity.getContent())); }