Search in sources :

Example 51 with JSONTokener

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) SharedPreferences(android.content.SharedPreferences) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) JSONException(org.json.JSONException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) BasicNameValuePair(org.apache.http.message.BasicNameValuePair)

Example 52 with JSONTokener

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;
    }
}
Also used : JSONTokener(org.json.JSONTokener) HttpEntity(org.apache.http.HttpEntity) JSONObject(org.json.JSONObject) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) JSONException(org.json.JSONException)

Example 53 with JSONTokener

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;
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject)

Example 54 with JSONTokener

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;
    }
}
Also used : JSONTokener(org.json.JSONTokener) InputStreamReader(java.io.InputStreamReader) JSONObject(org.json.JSONObject) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) JSONException(org.json.JSONException)

Example 55 with JSONTokener

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);
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) IOException(java.io.IOException)

Aggregations

JSONTokener (org.json.JSONTokener)63 JSONObject (org.json.JSONObject)60 JSONException (org.json.JSONException)32 JSONArray (org.json.JSONArray)23 IOException (java.io.IOException)19 ArrayList (java.util.ArrayList)12 InputStream (java.io.InputStream)10 GraphObject (com.facebook.model.GraphObject)8 FileInputStream (java.io.FileInputStream)8 File (java.io.File)6 HashMap (java.util.HashMap)6 HttpEntity (org.apache.http.HttpEntity)5 HttpResponse (org.apache.http.HttpResponse)5 BufferedReader (java.io.BufferedReader)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStreamReader (java.io.InputStreamReader)4 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)4 HttpPost (org.apache.http.client.methods.HttpPost)4 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)4 FacebookException (com.facebook.FacebookException)3