Search in sources :

Example 11 with JSONTokener

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

Example 12 with JSONTokener

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;
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) XmlDom(com.androidquery.util.XmlDom) JSONArray(org.json.JSONArray) XmlPullParser(org.xmlpull.v1.XmlPullParser) File(java.io.File) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 13 with JSONTokener

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

Example 14 with JSONTokener

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);
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AjaxCallback(com.androidquery.callback.AjaxCallback) AbstractAjaxCallback(com.androidquery.callback.AbstractAjaxCallback) BitmapAjaxCallback(com.androidquery.callback.BitmapAjaxCallback) AjaxStatus(com.androidquery.callback.AjaxStatus) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException)

Example 15 with JSONTokener

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

Aggregations

JSONTokener (org.json.JSONTokener)62 JSONObject (org.json.JSONObject)59 JSONException (org.json.JSONException)31 JSONArray (org.json.JSONArray)23 IOException (java.io.IOException)18 ArrayList (java.util.ArrayList)12 InputStream (java.io.InputStream)9 GraphObject (com.facebook.model.GraphObject)8 FileInputStream (java.io.FileInputStream)7 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