List of usage examples for org.json JSONException toString
public String toString()
From source file:com.altcanvas.asocial.Twitter.java
public JSONObject verifyCreds() throws AsocialException { try {/*from w w w . j a v a 2 s . c om*/ JSONObject json = new JSONObject(Http.get(twitterURL + "account/verify_credentials.json", headers)); // Authentication failed if (json.optString("error", null) != null) return null; return json; } catch (HttpException he) { throw new AsocialException(he.responseCode, he.toString()); } catch (JSONException je) { throw new AsocialException(AsocialException.JSONE, je.toString()); } catch (IOException ioe) { throw new AsocialException(AsocialException.IOE, ioe.toString()); } }
From source file:net.micode.notes.gtask.remote.GTaskClient.java
private boolean loginGtask(String authToken) { int timeoutConnection = 10000; int timeoutSocket = 15000; HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); mHttpClient = new DefaultHttpClient(httpParameters); BasicCookieStore localBasicCookieStore = new BasicCookieStore(); mHttpClient.setCookieStore(localBasicCookieStore); HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false); // login gtask try {/*from w ww. java2 s. c o m*/ String loginUrl = mGetUrl + "?auth=" + authToken; HttpGet httpGet = new HttpGet(loginUrl); HttpResponse response = null; response = mHttpClient.execute(httpGet); // get the cookie now List<Cookie> cookies = mHttpClient.getCookieStore().getCookies(); boolean hasAuthCookie = false; for (Cookie cookie : cookies) { if (cookie.getName().contains("GTL")) { hasAuthCookie = true; } } if (!hasAuthCookie) { Log.w(TAG, "it seems that there is no auth cookie"); } // get the client version String resString = getResponseContent(response.getEntity()); String jsBegin = "_setup("; String jsEnd = ")}</script>"; int begin = resString.indexOf(jsBegin); int end = resString.lastIndexOf(jsEnd); String jsString = null; if (begin != -1 && end != -1 && begin < end) { jsString = resString.substring(begin + jsBegin.length(), end); } JSONObject js = new JSONObject(jsString); mClientVersion = js.getLong("v"); } catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); return false; } catch (Exception e) { // simply catch all exceptions Log.e(TAG, "httpget gtask_url failed"); return false; } return true; }
From source file:net.micode.notes.gtask.remote.GTaskClient.java
public void createTask(Task task) throws NetworkFailureException { commitUpdate();//from ww w .j a va 2 s . co m try { JSONObject jsPost = new JSONObject(); JSONArray actionList = new JSONArray(); // action_list actionList.put(task.getCreateAction(getActionId())); jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // client_version jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); // post JSONObject jsResponse = postRequest(jsPost); JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_RESULTS).get(0); task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID)); } catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("create task: handing jsonobject failed"); } }
From source file:net.micode.notes.gtask.remote.GTaskClient.java
public void createTaskList(TaskList tasklist) throws NetworkFailureException { commitUpdate();// w ww. jav a 2 s. c om try { JSONObject jsPost = new JSONObject(); JSONArray actionList = new JSONArray(); // action_list actionList.put(tasklist.getCreateAction(getActionId())); jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // client version jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); // post JSONObject jsResponse = postRequest(jsPost); JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_RESULTS).get(0); tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID)); } catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("create tasklist: handing jsonobject failed"); } }
From source file:net.micode.notes.gtask.remote.GTaskClient.java
public void commitUpdate() throws NetworkFailureException { if (mUpdateArray != null) { try {//from w w w . j a v a 2 s . c om JSONObject jsPost = new JSONObject(); // action_list jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray); // client_version jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); postRequest(jsPost); mUpdateArray = null; } catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("commit update: handing jsonobject failed"); } } }
From source file:net.micode.notes.gtask.remote.GTaskClient.java
public void moveTask(Task task, TaskList preParent, TaskList curParent) throws NetworkFailureException { commitUpdate();//w w w.j a v a 2 s. c om try { JSONObject jsPost = new JSONObject(); JSONArray actionList = new JSONArray(); JSONObject action = new JSONObject(); // action_list action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_MOVE); action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId()); action.put(GTaskStringUtils.GTASK_JSON_ID, task.getGid()); if (preParent == curParent && task.getPriorSibling() != null) { // put prioring_sibing_id only if moving within the tasklist and // it is not the first one action.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, task.getPriorSibling()); } action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid()); action.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT, curParent.getGid()); if (preParent != curParent) { // put the dest_list only if moving between tasklists action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid()); } actionList.put(action); jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // client_version jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); postRequest(jsPost); } catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("move task: handing jsonobject failed"); } }
From source file:net.micode.notes.gtask.remote.GTaskClient.java
public void deleteNode(Node node) throws NetworkFailureException { commitUpdate();/*from w w w . j a va 2 s . c o m*/ try { JSONObject jsPost = new JSONObject(); JSONArray actionList = new JSONArray(); // action_list node.setDeleted(true); actionList.put(node.getUpdateAction(getActionId())); jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // client_version jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); postRequest(jsPost); mUpdateArray = null; } catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("delete node: handing jsonobject failed"); } }
From source file:net.micode.notes.gtask.remote.GTaskClient.java
public JSONArray getTaskList(String listGid) throws NetworkFailureException { commitUpdate();//from w w w. j a va 2s .com try { JSONObject jsPost = new JSONObject(); JSONArray actionList = new JSONArray(); JSONObject action = new JSONObject(); // action_list action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL); action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId()); action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid); action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false); actionList.put(action); jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // client_version jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); JSONObject jsResponse = postRequest(jsPost); return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS); } catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("get task list: handing jsonobject failed"); } }
From source file:com.piusvelte.sonet.SonetCreatePost.java
private void setLocation(final long accountId) { final ProgressDialog loadingDialog = new ProgressDialog(this); final AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() { int serviceId; @Override/*from w w w .jav a2 s. c o m*/ protected String doInBackground(Void... none) { Cursor account = getContentResolver().query(Accounts.getContentUri(SonetCreatePost.this), new String[] { Accounts._ID, Accounts.TOKEN, Accounts.SERVICE, Accounts.SECRET }, Accounts._ID + "=?", new String[] { Long.toString(accountId) }, null); if (account.moveToFirst()) { SonetOAuth sonetOAuth; serviceId = account.getInt(account.getColumnIndex(Accounts.SERVICE)); switch (serviceId) { case TWITTER: // anonymous requests are rate limited to 150 per hour // authenticated requests are rate limited to 350 per hour, so authenticate this! sonetOAuth = new SonetOAuth(BuildConfig.TWITTER_KEY, BuildConfig.TWITTER_SECRET, mSonetCrypto.Decrypt(account.getString(account.getColumnIndex(Accounts.TOKEN))), mSonetCrypto.Decrypt(account.getString(account.getColumnIndex(Accounts.SECRET)))); return SonetHttpClient.httpResponse(mHttpClient, sonetOAuth.getSignedRequest( new HttpGet(String.format(TWITTER_SEARCH, TWITTER_BASE_URL, mLat, mLong)))); case FACEBOOK: return SonetHttpClient.httpResponse(mHttpClient, new HttpGet(String.format(FACEBOOK_SEARCH, FACEBOOK_BASE_URL, mLat, mLong, Saccess_token, mSonetCrypto.Decrypt(account.getString(account.getColumnIndex(Accounts.TOKEN)))))); case FOURSQUARE: return SonetHttpClient.httpResponse(mHttpClient, new HttpGet(String.format( FOURSQUARE_SEARCH, FOURSQUARE_BASE_URL, mLat, mLong, mSonetCrypto.Decrypt(account.getString(account.getColumnIndex(Accounts.TOKEN)))))); } } account.close(); return null; } @Override protected void onPostExecute(String response) { if (loadingDialog.isShowing()) loadingDialog.dismiss(); if (response != null) { switch (serviceId) { case TWITTER: try { JSONArray places = new JSONObject(response).getJSONObject(Sresult) .getJSONArray(Splaces); final String placesNames[] = new String[places.length()]; final String placesIds[] = new String[places.length()]; for (int i = 0, i2 = places.length(); i < i2; i++) { JSONObject place = places.getJSONObject(i); placesNames[i] = place.getString(Sfull_name); placesIds[i] = place.getString(Sid); } mDialog = (new AlertDialog.Builder(SonetCreatePost.this)) .setSingleChoiceItems(placesNames, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mAccountsLocation.put(accountId, placesIds[which]); dialog.dismiss(); } }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }) .create(); mDialog.show(); } catch (JSONException e) { Log.e(TAG, e.toString()); } break; case FACEBOOK: try { JSONArray places = new JSONObject(response).getJSONArray(Sdata); final String placesNames[] = new String[places.length()]; final String placesIds[] = new String[places.length()]; for (int i = 0, i2 = places.length(); i < i2; i++) { JSONObject place = places.getJSONObject(i); placesNames[i] = place.getString(Sname); placesIds[i] = place.getString(Sid); } mDialog = (new AlertDialog.Builder(SonetCreatePost.this)) .setSingleChoiceItems(placesNames, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mAccountsLocation.put(accountId, placesIds[which]); dialog.dismiss(); } }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }) .create(); mDialog.show(); } catch (JSONException e) { Log.e(TAG, e.toString()); } break; case FOURSQUARE: try { JSONArray groups = new JSONObject(response).getJSONObject(Sresponse) .getJSONArray(Sgroups); for (int g = 0, g2 = groups.length(); g < g2; g++) { JSONObject group = groups.getJSONObject(g); if (group.getString(Sname).equals(SNearby)) { JSONArray places = group.getJSONArray(Sitems); final String placesNames[] = new String[places.length()]; final String placesIds[] = new String[places.length()]; for (int i = 0, i2 = places.length(); i < i2; i++) { JSONObject place = places.getJSONObject(i); placesNames[i] = place.getString(Sname); placesIds[i] = place.getString(Sid); } mDialog = (new AlertDialog.Builder(SonetCreatePost.this)) .setSingleChoiceItems(placesNames, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mAccountsLocation.put(accountId, placesIds[which]); dialog.dismiss(); } }) .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }) .create(); mDialog.show(); break; } } } catch (JSONException e) { Log.e(TAG, e.toString()); } break; } } else { (Toast.makeText(SonetCreatePost.this, getString(R.string.failure), Toast.LENGTH_LONG)).show(); } } }; loadingDialog.setMessage(getString(R.string.loading)); loadingDialog.setCancelable(true); loadingDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { if (!asyncTask.isCancelled()) asyncTask.cancel(true); } }); loadingDialog.setButton(ProgressDialog.BUTTON_NEGATIVE, getString(android.R.string.cancel), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); loadingDialog.show(); asyncTask.execute(); }
From source file:com.ofalvai.bpinfo.api.bkkinfo.BkkInfoClient.java
@NonNull private List<Alert> parseTodayAlerts(JSONObject response) throws JSONException { List<Alert> alerts = new ArrayList<>(); boolean isDebugMode = mSharedPreferences.getBoolean(mContext.getString(R.string.pref_key_debug_mode), false);// w w w . ja v a 2 s . c om JSONArray activeAlertsList = response.getJSONArray("active"); for (int i = 0; i < activeAlertsList.length(); i++) { try { JSONObject alertNode = activeAlertsList.getJSONObject(i); Alert alert = parseAlert(alertNode); // Some alerts are still listed a few minutes after they ended, we need to hide them, // but still show them if debug mode is enabled DateTime alertEndTime = new DateTime(alert.getEnd() * 1000L); if (alertEndTime.isAfterNow() || alert.getEnd() == 0 || isDebugMode) { alerts.add(parseAlert(alertNode)); } } catch (JSONException ex) { Crashlytics.log(Log.WARN, TAG, "Alert parse: failed to parse:\n" + ex.toString()); } } return alerts; }