Search in sources :

Example 46 with JSONTokener

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

Example 47 with JSONTokener

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

Example 48 with JSONTokener

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();
    }
}
Also used : JSONTokener(org.json.JSONTokener) ApplicationGroup(com.netflix.ice.reader.ApplicationGroup) JSONObject(org.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JSONException(org.json.JSONException) JSONException(org.json.JSONException)

Example 49 with JSONTokener

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

Example 50 with JSONTokener

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();
    }
}
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) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) JSONException(org.json.JSONException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) JSONTokener(org.json.JSONTokener) CookieStore(org.apache.http.client.CookieStore) JSONObject(org.json.JSONObject) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) BufferedReader(java.io.BufferedReader)

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