use of org.json.JSONTokener in project openkit-android by OpenKit.
the class JsonHttpResponseHandler method parseResponse.
protected Object parseResponse(String responseBody) throws JSONException {
Object result = null;
//trim the string to prevent start with blank, and test if the string is valid JSON, because the parser don't do this :(. If Json is not valid this will return null
responseBody = responseBody.trim();
if (responseBody.startsWith("{") || responseBody.startsWith("[")) {
result = new JSONTokener(responseBody).nextValue();
}
if (result == null) {
result = responseBody;
}
return result;
}
use of org.json.JSONTokener in project openkit-android by OpenKit.
the class Utility method getStringPropertyAsJSON.
// Returns either a JSONObject or JSONArray representation of the 'key' property of 'jsonObject'.
public static Object getStringPropertyAsJSON(JSONObject jsonObject, String key, String nonJSONPropertyKey) throws JSONException {
Object value = jsonObject.opt(key);
if (value != null && value instanceof String) {
JSONTokener tokener = new JSONTokener((String) value);
value = tokener.nextValue();
}
if (value != null && !(value instanceof JSONObject || value instanceof JSONArray)) {
if (nonJSONPropertyKey != null) {
// Facebook sometimes gives us back a non-JSON value such as
// literal "true" or "false" as a result.
// If we got something like that, we present it to the caller as
// a GraphObject with a single
// property. We only do this if the caller wants that behavior.
jsonObject = new JSONObject();
jsonObject.putOpt(nonJSONPropertyKey, value);
return jsonObject;
} else {
throw new FacebookException("Got an unexpected non-JSON object.");
}
}
return value;
}
use of org.json.JSONTokener in project ice by Netflix.
the class BasicS3ApplicationGroupService method getApplicationGroups.
public Map<String, ApplicationGroup> getApplicationGroups() {
String jsonStr;
try {
InputStream in = s3Client.getObject(config.workS3BucketName, config.workS3BucketPrefix + "appgroups").getObjectContent();
jsonStr = IOUtils.toString(in);
in.close();
} catch (Exception e) {
logger.error("Error reading from appgroups file", e);
try {
InputStream in = s3Client.getObject(config.workS3BucketName, config.workS3BucketPrefix + "copy_appgroups").getObjectContent();
jsonStr = IOUtils.toString(in);
in.close();
} catch (Exception r) {
logger.error("Error reading from copy_appgroups file", r);
return Maps.newHashMap();
}
}
try {
JSONObject json = new JSONObject(new JSONTokener(jsonStr));
Map<String, ApplicationGroup> appgroups = Maps.newHashMap();
Iterator<String> keys = json.keys();
while (keys.hasNext()) {
String key = keys.next();
String str = json.getString(key);
appgroups.put(key, new ApplicationGroup(str));
}
return appgroups;
} catch (JSONException e) {
logger.error("Error reading appgroups from json...", e);
return Maps.newHashMap();
}
}
use of org.json.JSONTokener in project android-instagram by markchang.
the class ImageListActivity method showShareDialog.
public void showShareDialog(InstagramImage image) {
final InstagramImage finalImage = image;
// get the permalink
String url = Utils.createPermalinkUrl(finalImage.pk);
String jsonResponse = Utils.doRestfulGet(httpClient, url, getApplicationContext());
if (jsonResponse != null) {
try {
JSONTokener jsonTokener = new JSONTokener(jsonResponse);
JSONObject jsonObject = new JSONObject(jsonTokener);
String permalink = jsonObject.getString("permalink");
if (permalink != null) {
// shoot the intent
// will default to "messaging / sms" if nothing else is installed
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
//Text seems to be necessary for Facebook and Twitter
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_TEXT, image.caption + " " + permalink);
startActivity(Intent.createChooser(sharingIntent, "Share using"));
}
} catch (JSONException j) {
Log.e(TAG, "JSON parse error: " + j.toString());
Toast.makeText(getApplicationContext(), "There was an error communicating with Instagram", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "Failed to get permalink for the image", Toast.LENGTH_SHORT).show();
}
}
use of org.json.JSONTokener in project android-instagram by markchang.
the class LoginActivity method doLogin.
public void doLogin(View view) {
CookieStore cookieStore;
// clear cookies
clearCookies();
// gather login info
String password = txtPassword.getText().toString();
String username = txtUsername.getText().toString();
if (Utils.isOnline(getApplicationContext()) == false) {
Toast.makeText(LoginActivity.this, "No connection to Internet.\nTry again later.", Toast.LENGTH_SHORT).show();
Log.i(Utils.TAG, "No internet, failed Login");
return;
}
// create POST
HttpPost httpPost = new HttpPost(Utils.LOGIN_URL);
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("username", username));
postParams.add(new BasicNameValuePair("password", password));
postParams.add(new BasicNameValuePair("device_id", "0000"));
try {
httpPost.setEntity(new UrlEncodedFormEntity(postParams, HTTP.UTF_8));
HttpResponse httpResponse = httpClient.execute(httpPost);
// test result code
if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
Toast.makeText(LoginActivity.this, "Login failed", Toast.LENGTH_SHORT).show();
Log.i(TAG, "Login HTTP status fail");
return;
}
// test json response
HttpEntity httpEntity = httpResponse.getEntity();
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")) {
Toast.makeText(LoginActivity.this, "Login failed", Toast.LENGTH_SHORT).show();
Log.e(TAG, "JSON status not ok: " + jsonObject.getString("status"));
return;
}
}
// save login info so that we can reuse them later
cookieStore = httpClient.getCookieStore();
if (saveLoginInfo(cookieStore, username, password) == true) {
Toast.makeText(LoginActivity.this, "Logged in", Toast.LENGTH_SHORT).show();
openMainActivity();
} else {
Toast.makeText(LoginActivity.this, "Login failed", Toast.LENGTH_SHORT).show();
Log.e(TAG, "Cookie error");
}
} catch (IOException e) {
Log.e(TAG, "HttpPost error: " + e.toString());
Toast.makeText(LoginActivity.this, "Login failed " + e.toString(), Toast.LENGTH_LONG).show();
} catch (JSONException e) {
Log.e(TAG, "JSON parse error: " + e.toString());
Toast.makeText(LoginActivity.this, "Result from instagr.am was unexpected: " + e.toString(), Toast.LENGTH_LONG).show();
}
}
Aggregations