Search in sources :

Example 81 with JSONException

use of org.json.JSONException in project android-instagram by markchang.

the class Utils method doRestulPut.

public static String doRestulPut(DefaultHttpClient httpClient, String url, List<NameValuePair> postParams, Context ctx) {
    // create POST
    HttpPost httpPost = new HttpPost(url);
    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 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")) {
                Log.e(TAG, "JSON status not ok: " + jsonObject.getString("status"));
                return null;
            } else {
                return json;
            }
        } else {
            return null;
        }
    } catch (IOException e) {
        Log.e(TAG, "HttpPost error: " + e.toString());
        return null;
    } catch (JSONException e) {
        Log.e(TAG, "JSON parse error: " + e.toString());
        return null;
    }
}
Also used : JSONTokener(org.json.JSONTokener) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) JSONObject(org.json.JSONObject) HttpResponse(org.apache.http.HttpResponse) JSONException(org.json.JSONException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity)

Example 82 with JSONException

use of org.json.JSONException in project QuickReturn by lawloretienne.

the class QuickReturnBaseActivity method onActivityResult.

// endregion
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == BUY_REQUEST_CODE) {
        int responseCode;
        switch(resultCode) {
            case RESULT_OK:
                Log.d(getClass().getSimpleName(), "onActivityResult() : RESULT_OK");
                responseCode = data.getIntExtra(RESPONSE_CODE, -5);
                switch(responseCode) {
                    case BILLING_RESPONSE_RESULT_OK:
                        String signature = data.getStringExtra(RESPONSE_INAPP_SIGNATURE);
                        String purchaseData = data.getStringExtra(RESPONSE_INAPP_PURCHASE_DATA);
                        JSONObject object;
                        try {
                            object = new JSONObject(purchaseData);
                            // sample data
                            // "purchaseToken" -> "inapp:com.etiennelawlor.quickreturn:android.test.purchased"
                            // "developerPayload" -> "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzK"
                            // "packageName" -> "com.etiennelawlor.quickreturn"
                            // "purchaseState" -> "0"
                            // "orderId" -> "transactionId.android.test.purchased"
                            // "purchaseTime" -> "0"
                            // "productId" -> "android.test.purchased"
                            //
                            String sku = object.getString("productId");
                            if (!TextUtils.isEmpty(sku)) {
                                if (sku.equals(getString(R.string.buy_one_beer))) {
                                    showCrouton(android.R.color.holo_green_light, getResources().getQuantityString(R.plurals.beer_cheers, 1, 1));
                                } else if (sku.equals(getString(R.string.buy_two_beers))) {
                                    showCrouton(android.R.color.holo_green_light, getResources().getQuantityString(R.plurals.beer_cheers, 2, 2));
                                } else if (sku.equals(getString(R.string.buy_four_beers))) {
                                    showCrouton(android.R.color.holo_green_light, getResources().getQuantityString(R.plurals.beer_cheers, 4, 4));
                                } else if (sku.equals("android.test.purchased")) {
                                    showCrouton(android.R.color.holo_green_light, "Test Purchase completed");
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        // consumption of the purchase here)
                        break;
                    case BILLING_RESPONSE_RESULT_USER_CANCELED:
                        Log.d(getClass().getSimpleName(), "donate() : User pressed back or canceled a dialog");
                        break;
                    case BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE:
                        Log.d(getClass().getSimpleName(), "donate() : Billing API version is not supported for the type requested");
                        break;
                    case BILLING_RESPONSE_RESULT_ITEM_UNAVAILABLE:
                        Log.d(getClass().getSimpleName(), "donate() : Requested product is not available for purchase");
                        break;
                    case BILLING_RESPONSE_RESULT_DEVELOPER_ERROR:
                        Log.d(getClass().getSimpleName(), "donate() : Invalid arguments provided to the API. This error can also " + "indicate that the application was not correctly signed or properly set up for In-app Billing in " + "Google Play, or does not have the necessary permissions in its manifest");
                        break;
                    case BILLING_RESPONSE_RESULT_ERROR:
                        Log.d(getClass().getSimpleName(), "donate() : Fatal error during the API action");
                        break;
                    case BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED:
                        Log.d(getClass().getSimpleName(), "donate() : Failure to purchase since item is already owned");
                        showCrouton(android.R.color.holo_red_light, R.string.item_already_owned);
                        break;
                    case BILLING_RESPONSE_RESULT_ITEM_NOT_OWNED:
                        Log.d(getClass().getSimpleName(), "donate() : Failure to consume since item is not owned");
                        break;
                    default:
                        break;
                }
                break;
            case RESULT_CANCELED:
                Log.d(getClass().getSimpleName(), "onActivityResult() : RESULT_CANCELED");
                //                    responseCode = data.getIntExtra(RESPONSE_CODE, -5);
                showCrouton(android.R.color.holo_red_light, R.string.beer_order_canceled);
                break;
            default:
                break;
        }
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException)

Example 83 with JSONException

use of org.json.JSONException in project QuickReturn by lawloretienne.

the class QuickReturnBaseActivity method getAllSkus.

private void getAllSkus() {
    ArrayList<String> skuList = new ArrayList<>();
    skuList.add("buy_one_beer");
    skuList.add("buy_two_beers");
    skuList.add("buy_four_beers");
    Bundle querySkus = new Bundle();
    querySkus.putStringArrayList(GET_SKU_DETAILS_ITEM_LIST, skuList);
    try {
        Bundle skuDetails = mService.getSkuDetails(3, getPackageName(), ITEM_TYPE_INAPP, querySkus);
        int response = skuDetails.getInt(RESPONSE_CODE);
        if (response == BILLING_RESPONSE_RESULT_OK) {
            ArrayList<String> responseList = skuDetails.getStringArrayList(RESPONSE_GET_SKU_DETAILS_LIST);
            for (String thisResponse : responseList) {
                JSONObject object;
                try {
                    object = new JSONObject(thisResponse);
                    String sku = object.getString("productId");
                    String price = object.getString("price");
                    if (sku.equals(getString(R.string.buy_one_beer))) {
                        Log.d(getClass().getSimpleName(), "price - " + price);
                    //                            mPremiumUpgradePrice = price;
                    } else if (sku.equals(getString(R.string.buy_two_beers))) {
                        Log.d(getClass().getSimpleName(), "price - " + price);
                    //                            mGasPrice = price;
                    } else if (sku.equals(getString(R.string.buy_four_beers))) {
                        Log.d(getClass().getSimpleName(), "price - " + price);
                    //                            mGasPrice = price;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}
Also used : JSONObject(org.json.JSONObject) Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) RemoteException(android.os.RemoteException)

Example 84 with JSONException

use of org.json.JSONException in project LiveSDK-for-Android by liveservices.

the class LiveConnectClient method put.

/**
     * Performs a synchronous HTTP PUT on the Live Connect REST API.
     *
     * A PUT updates a resource or if it does not exist, it creates a one.
     *
     * @param path object_id of the put request.
     * @param body body of the put request.
     * @return a LiveOperation that contains the JSON result.
     * @throws LiveOperationException if there is an error during the execution of the request.
     * @throws IllegalArgumentException if the path is empty or is an absolute uri.
     * @throws NullPointerException if either the path or body parameters are null.
     */
public LiveOperation put(String path, String body) throws LiveOperationException {
    LiveConnectUtils.assertNotNullOrEmpty(body, ParamNames.BODY);
    JSONObject jsonBody;
    try {
        jsonBody = new JSONObject(body.toString());
    } catch (JSONException e) {
        throw new LiveOperationException(ErrorMessages.CLIENT_ERROR, e);
    }
    return this.put(path, jsonBody);
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException)

Example 85 with JSONException

use of org.json.JSONException in project LiveSDK-for-Android by liveservices.

the class LiveConnectClient method putAsync.

/**
     * Performs an asynchronous HTTP PUT on the Live Connect REST API.
     *
     * A PUT updates a resource or if it does not exist, it creates a one.
     *
     * {@link LiveOperationListener#onComplete(LiveOperation)} will be called on success.
     * Otherwise, {@link LiveOperationListener#onError(LiveOperationException, LiveOperation)} will
     * be called. Both of these methods will be called on the main/UI thread.
     *
     * @param path object_id of the put request.
     * @param body body of the put request.
     * @param listener called on either completion or error during the put request.
     * @param userState arbitrary object that is used to determine the caller of the method.
     * @return the LiveOperation associated with the request.
     * @throws IllegalArgumentException if the path is empty or is an absolute uri.
     * @throws NullPointerException if either the path or body parameters are null.
     */
public LiveOperation putAsync(String path, String body, LiveOperationListener listener, Object userState) {
    LiveConnectUtils.assertNotNullOrEmpty(body, ParamNames.BODY);
    JSONObject jsonBody;
    try {
        jsonBody = new JSONObject(body.toString());
    } catch (JSONException e) {
        return handleException(PutRequest.METHOD, path, new LiveOperationException(ErrorMessages.CLIENT_ERROR, e), listener, userState);
    }
    return this.putAsync(path, jsonBody, listener, userState);
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException)

Aggregations

JSONException (org.json.JSONException)1904 JSONObject (org.json.JSONObject)1422 JSONArray (org.json.JSONArray)638 IOException (java.io.IOException)338 ArrayList (java.util.ArrayList)211 HashMap (java.util.HashMap)145 Test (org.junit.Test)128 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)98 File (java.io.File)77 Bundle (android.os.Bundle)75 VolleyError (com.android.volley.VolleyError)67 Intent (android.content.Intent)63 DefaultGNSTest (edu.umass.cs.gnsserver.utils.DefaultGNSTest)62 Response (com.android.volley.Response)59 URL (java.net.URL)57 Map (java.util.Map)48 TextView (android.widget.TextView)46 View (android.view.View)45 RandomString (edu.umass.cs.gnscommon.utils.RandomString)44 MalformedURLException (java.net.MalformedURLException)44