use of org.json.JSONTokener in project muzei by romannurik.
the class FeaturedArtSource method fetchJsonObject.
private JSONObject fetchJsonObject(final String url) throws IOException, JSONException {
OkHttpClient client = new OkHttpClient.Builder().build();
Request request = new Request.Builder().url(url).build();
String json = client.newCall(request).execute().body().string();
JSONTokener tokener = new JSONTokener(json);
Object val = tokener.nextValue();
if (!(val instanceof JSONObject)) {
throw new JSONException("Expected JSON object.");
}
return (JSONObject) val;
}
use of org.json.JSONTokener in project androidquery by androidquery.
the class AbstractAjaxCallback method transform.
@SuppressWarnings("unchecked")
protected T transform(String url, byte[] data, AjaxStatus status) {
if (type == null) {
return null;
}
File file = status.getFile();
if (data != null) {
if (type.equals(Bitmap.class)) {
return (T) BitmapFactory.decodeByteArray(data, 0, data.length);
}
if (type.equals(JSONObject.class)) {
JSONObject result = null;
String str = null;
try {
str = new String(data, encoding);
result = (JSONObject) new JSONTokener(str).nextValue();
} catch (Exception e) {
AQUtility.debug(e);
AQUtility.debug(str);
}
return (T) result;
}
if (type.equals(JSONArray.class)) {
JSONArray result = null;
try {
String str = new String(data, encoding);
result = (JSONArray) new JSONTokener(str).nextValue();
} catch (Exception e) {
AQUtility.debug(e);
}
return (T) result;
}
if (type.equals(String.class)) {
String result = null;
if (status.getSource() == AjaxStatus.NETWORK) {
AQUtility.debug("network");
result = correctEncoding(data, encoding, status);
} else {
AQUtility.debug("file");
try {
result = new String(data, encoding);
} catch (Exception e) {
AQUtility.debug(e);
}
}
return (T) result;
}
if (type.equals(byte[].class)) {
return (T) data;
}
if (transformer != null) {
return transformer.transform(url, type, encoding, data, status);
}
if (st != null) {
return st.transform(url, type, encoding, data, status);
}
} else if (file != null) {
if (type.equals(File.class)) {
return (T) file;
}
if (type.equals(XmlDom.class)) {
XmlDom result = null;
try {
FileInputStream fis = new FileInputStream(file);
result = new XmlDom(fis);
status.closeLater(fis);
} catch (Exception e) {
AQUtility.report(e);
return null;
}
return (T) result;
}
if (type.equals(XmlPullParser.class)) {
XmlPullParser parser = Xml.newPullParser();
try {
FileInputStream fis = new FileInputStream(file);
parser.setInput(fis, encoding);
status.closeLater(fis);
} catch (Exception e) {
AQUtility.report(e);
return null;
}
return (T) parser;
}
if (type.equals(InputStream.class)) {
try {
FileInputStream fis = new FileInputStream(file);
status.closeLater(fis);
return (T) fis;
} catch (Exception e) {
AQUtility.report(e);
return null;
}
}
}
return null;
}
use of org.json.JSONTokener in project simplefacebook by androidquery.
the class PTransformer method transform.
@Override
public <T> T transform(String url, Class<T> type, String encoding, byte[] data, AjaxStatus status) {
if (type.equals(Feed.class)) {
Feed result = null;
try {
String str = new String(data, encoding);
JSONObject jo = (JSONObject) new JSONTokener(str).nextValue();
result = new Feed(jo);
} catch (Exception e) {
AQUtility.debug(e);
}
return (T) result;
}
return null;
}
use of org.json.JSONTokener in project androidquery by androidquery.
the class AQueryAsyncTest method testWaitBlockInputStream.
public void testWaitBlockInputStream() {
String url = "http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0";
AjaxCallback<InputStream> cb = new AjaxCallback<InputStream>();
cb.url(url).type(InputStream.class);
aq.sync(cb);
String u = cb.getUrl();
InputStream is = cb.getResult();
AjaxStatus status = cb.getStatus();
byte[] data = AQUtility.toBytes(is);
JSONObject jo = null;
String str = null;
try {
str = new String(data, "UTF-8");
jo = (JSONObject) new JSONTokener(str).nextValue();
} catch (Exception e) {
AQUtility.debug(e);
AQUtility.debug(str);
}
assertNotNull(jo);
assertNotNull(jo.opt("responseData"));
checkStatus(status);
}
use of org.json.JSONTokener in project android-instagram by markchang.
the class TakePictureActivity method doUpload.
// fixme: this doesn't need to be a Map return
public Map<String, String> doUpload() {
Log.i(TAG, "Upload");
Long timeInMilliseconds = System.currentTimeMillis() / 1000;
String timeInSeconds = timeInMilliseconds.toString();
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
Map returnMap = new HashMap<String, String>();
// check for cookies
if (httpClient.getCookieStore() == null) {
returnMap.put("result", "Not logged in");
return returnMap;
}
try {
// create multipart data
File imageFile = new File(processedImageUri.getPath());
FileBody partFile = new FileBody(imageFile);
StringBody partTime = new StringBody(timeInSeconds);
multipartEntity.addPart("photo", partFile);
multipartEntity.addPart("device_timestamp", partTime);
} catch (Exception e) {
Log.e(TAG, "Error creating mulitpart form: " + e.toString());
returnMap.put("result", "Error creating mulitpart form: " + e.toString());
return returnMap;
}
// upload
try {
HttpPost httpPost = new HttpPost(Utils.UPLOAD_URL);
httpPost.setEntity(multipartEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
Log.i(TAG, "Upload status: " + httpResponse.getStatusLine());
// test result code
if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
Log.e(TAG, "Login HTTP status fail: " + httpResponse.getStatusLine().getStatusCode());
returnMap.put("result", "HTTP status error: " + httpResponse.getStatusLine().getStatusCode());
return returnMap;
}
/*
{"status": "ok"}
*/
if (httpEntity != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(httpEntity.getContent(), "UTF-8"));
String json = reader.readLine();
JSONTokener jsonTokener = new JSONTokener(json);
JSONObject jsonObject = new JSONObject(jsonTokener);
Log.i(TAG, "JSON: " + jsonObject.toString());
String loginStatus = jsonObject.getString("status");
if (!loginStatus.equals("ok")) {
Log.e(TAG, "JSON status not ok: " + jsonObject.getString("status"));
returnMap.put("result", "JSON status not ok: " + jsonObject.getString("status"));
return returnMap;
}
}
} catch (Exception e) {
Log.e(TAG, "HttpPost exception: " + e.toString());
returnMap.put("result", "HttpPost exception: " + e.toString());
return returnMap;
}
// configure / comment
try {
HttpPost httpPost = new HttpPost(Utils.CONFIGURE_URL);
String partComment = txtCaption.getText().toString();
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("device_timestamp", timeInSeconds));
postParams.add(new BasicNameValuePair("caption", partComment));
httpPost.setEntity(new UrlEncodedFormEntity(postParams, HTTP.UTF_8));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
// test result code
if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
Log.e(TAG, "Upload comment fail: " + httpResponse.getStatusLine().getStatusCode());
returnMap.put("result", "Upload comment fail: " + httpResponse.getStatusLine().getStatusCode());
return returnMap;
}
returnMap.put("result", "ok");
return returnMap;
} catch (Exception e) {
Log.e(TAG, "HttpPost comment error: " + e.toString());
returnMap.put("result", "HttpPost comment error: " + e.toString());
return returnMap;
}
}
Aggregations