use of org.json.JSONTokener in project android-instagram by markchang.
the class Utils method doLogin.
public static boolean doLogin(Context ctx, DefaultHttpClient httpClient) {
Log.i(TAG, "Doing login");
// gather login info
SharedPreferences sharedPreferences = ctx.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
Boolean loginValid = sharedPreferences.getBoolean("loginValid", false);
if (!loginValid) {
launchCredentials(ctx);
return false;
}
String username = sharedPreferences.getString("username", "");
String password = sharedPreferences.getString("password", "");
// create POST
HttpPost httpPost = new HttpPost(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) {
Log.i(TAG, "Login HTTP status fail");
return false;
}
// 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")) {
Log.e(TAG, "JSON status not ok: " + jsonObject.getString("status"));
return false;
}
}
} catch (IOException e) {
Log.e(TAG, "HttpPost error: " + e.toString());
return false;
} catch (JSONException e) {
Log.e(TAG, "JSON parse error: " + e.toString());
return false;
}
return true;
}
use of org.json.JSONTokener in project android-instagram by markchang.
the class Utils method doRestfulGet.
public static String doRestfulGet(DefaultHttpClient httpClient, String url, Context ctx) {
Log.i(Utils.TAG, "Image fetch");
if (Utils.isOnline(ctx) == false) {
Toast.makeText(ctx, "No connection to Internet.\nTry again later", Toast.LENGTH_SHORT).show();
Log.i(Utils.TAG, "No internet!");
return null;
}
try {
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
// test result code
if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
Toast.makeText(ctx, "Action failed.", Toast.LENGTH_SHORT).show();
Log.e(TAG, "Return status code bad.");
return null;
}
// 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(ctx, "Network activity did not return ok", Toast.LENGTH_SHORT).show();
Log.e(TAG, "JSON status not ok: " + jsonObject.getString("status"));
return null;
} else {
return json;
}
} else {
Toast.makeText(ctx, "Improper data returned from Instagram", Toast.LENGTH_SHORT).show();
Log.e(TAG, "instagram returned bad data");
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
use of org.json.JSONTokener in project SeaStar by 13120241790.
the class JsonHttpResponseHandler method parseResponse.
protected Object parseResponse(String responseBody) throws JSONException {
if (null == responseBody)
return null;
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
String jsonString = responseBody.trim();
if (jsonString.startsWith("{") || jsonString.startsWith("[")) {
result = new JSONTokener(jsonString).nextValue();
}
if (result == null) {
result = jsonString;
}
return result;
}
use of org.json.JSONTokener in project PD-classes by watabou.
the class Bundle method read.
public static Bundle read(InputStream stream) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
StringBuilder all = new StringBuilder();
String line = reader.readLine();
while (line != null) {
all.append(line);
line = reader.readLine();
}
JSONObject json = (JSONObject) new JSONTokener(all.toString()).nextValue();
reader.close();
return new Bundle(json);
} catch (Exception e) {
return null;
}
}
use of org.json.JSONTokener in project BarcodeEye by BarcodeEye.
the class BookResultInfoRetriever method retrieveSupplementalInfo.
@Override
void retrieveSupplementalInfo() throws IOException {
CharSequence contents = HttpHelper.downloadViaHttp("https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbn, HttpHelper.ContentType.JSON);
if (contents.length() == 0) {
return;
}
String title;
String pages;
Collection<String> authors = null;
try {
JSONObject topLevel = (JSONObject) new JSONTokener(contents.toString()).nextValue();
JSONArray items = topLevel.optJSONArray("items");
if (items == null || items.isNull(0)) {
return;
}
JSONObject volumeInfo = ((JSONObject) items.get(0)).getJSONObject("volumeInfo");
if (volumeInfo == null) {
return;
}
title = volumeInfo.optString("title");
pages = volumeInfo.optString("pageCount");
JSONArray authorsArray = volumeInfo.optJSONArray("authors");
if (authorsArray != null && !authorsArray.isNull(0)) {
authors = new ArrayList<String>(authorsArray.length());
for (int i = 0; i < authorsArray.length(); i++) {
authors.add(authorsArray.getString(i));
}
}
} catch (JSONException e) {
throw new IOException(e);
}
Collection<String> newTexts = new ArrayList<String>();
maybeAddText(title, newTexts);
maybeAddTextSeries(authors, newTexts);
maybeAddText(pages == null || pages.isEmpty() ? null : pages + "pp.", newTexts);
String baseBookUri = "http://www.google." + LocaleManager.getBookSearchCountryTLD(context) + "/search?tbm=bks&source=zxing&q=";
append(isbn, source, newTexts.toArray(new String[newTexts.size()]), baseBookUri + isbn);
}
Aggregations