Example usage for org.json JSONException toString

List of usage examples for org.json JSONException toString

Introduction

In this page you can find the example usage for org.json JSONException toString.

Prototype

public String toString() 

Source Link

Document

Returns a short description of this throwable.

Usage

From source file:com.evothings.BLE.java

private void readCharacteristic(final CordovaArgs args, final CallbackContext callbackContext)
        throws JSONException {
    final GattHandler gh = mGatt.get(args.getInt(0));
    gh.mOperations.add(new Runnable() {
        @Override/*from www.  j a  v a 2s  . co m*/
        public void run() {
            try {
                gh.mCurrentOpContext = callbackContext;
                if (!gh.mGatt.readCharacteristic(gh.mCharacteristics.get(args.getInt(1)))) {
                    gh.mCurrentOpContext = null;
                    callbackContext.error("readCharacteristic");
                    gh.process();
                }
            } catch (JSONException e) {
                e.printStackTrace();
                callbackContext.error(e.toString());
                gh.process();
            }
        }
    });
    gh.process();
}

From source file:com.evothings.BLE.java

private void readDescriptor(final CordovaArgs args, final CallbackContext callbackContext)
        throws JSONException {
    final GattHandler gh = mGatt.get(args.getInt(0));
    gh.mOperations.add(new Runnable() {
        @Override// w  w w  .  j a va2  s.  c om
        public void run() {
            try {
                gh.mCurrentOpContext = callbackContext;
                if (!gh.mGatt.readDescriptor(gh.mDescriptors.get(args.getInt(1)))) {
                    gh.mCurrentOpContext = null;
                    callbackContext.error("readDescriptor");
                    gh.process();
                }
            } catch (JSONException e) {
                e.printStackTrace();
                callbackContext.error(e.toString());
                gh.process();
            }
        }
    });
    gh.process();
}

From source file:com.evothings.BLE.java

private void writeCharacteristic(final CordovaArgs args, final CallbackContext callbackContext)
        throws JSONException {
    final GattHandler gh = mGatt.get(args.getInt(0));
    gh.mOperations.add(new Runnable() {
        @Override/*w  w w  . j  a v  a2 s .  c  o  m*/
        public void run() {
            try {
                gh.mCurrentOpContext = callbackContext;
                BluetoothGattCharacteristic c = gh.mCharacteristics.get(args.getInt(1));
                System.out.println("writeCharacteristic(" + args.getInt(0) + ", " + args.getInt(1) + ", "
                        + args.getString(2) + ")");
                c.setValue(args.getArrayBuffer(2));
                if (!gh.mGatt.writeCharacteristic(c)) {
                    gh.mCurrentOpContext = null;
                    callbackContext.error("writeCharacteristic");
                    gh.process();
                }
            } catch (JSONException e) {
                e.printStackTrace();
                callbackContext.error(e.toString());
                gh.process();
            }
        }
    });
    gh.process();
}

From source file:com.evothings.BLE.java

private void writeDescriptor(final CordovaArgs args, final CallbackContext callbackContext)
        throws JSONException {
    final GattHandler gh = mGatt.get(args.getInt(0));
    gh.mOperations.add(new Runnable() {
        @Override/*  www  . ja v a  2s.c o  m*/
        public void run() {
            try {
                gh.mCurrentOpContext = callbackContext;
                BluetoothGattDescriptor d = gh.mDescriptors.get(args.getInt(1));
                d.setValue(args.getArrayBuffer(2));
                if (!gh.mGatt.writeDescriptor(d)) {
                    gh.mCurrentOpContext = null;
                    callbackContext.error("writeDescriptor");
                    gh.process();
                }
            } catch (JSONException e) {
                e.printStackTrace();
                callbackContext.error(e.toString());
                gh.process();
            }
        }
    });
    gh.process();
}

From source file:com.tmm.android.facebook.HelloFacebookSampleActivity.java

private void getpost() {
    try {/*from  www  . ja v  a2 s  .c om*/

        /*   Session.openActiveSessionWithAccessToken(this, token, new Session.StatusCallback() {
                
              @Override
              public void call(Session session, SessionState state, Exception exception) {
                 // TODO Auto-generated method stub
        */
        /*if (Session.isOpened()) {*/

        Session session = Session.getActiveSession();
        Log.d("Graph API", " 1 Graph API");
        Request.newGraphPathRequest(session, "me/home", new Request.Callback() {
            @Override
            public void onCompleted(Response response) {
                // TODO Auto-generated
                // method stub
                Log.d("Graph API", "On complete");
                JSONObject jsonObject = null;
                JSONArray jArray = null;
                try {

                    jsonObject = new JSONObject(response.getGraphObject().getInnerJSONObject().toString());
                    jArray = jsonObject.getJSONArray("data");
                    for (int i = 0; i < jArray.length(); i++) {
                        JSONObject element = null;
                        element = jArray.getJSONObject(i);
                        Log.d("Graph API", "Graph API element");
                        System.out.println(element.get("id") + "\n");
                    }
                } catch (JSONException e) {
                    // TODO: handle
                    // exception,,
                    System.out.println("JSON EXCEPTION:" + e);
                }
            }
        });
        /*} else {
          System.out .println("KONEKCIJA NIJE OTVORENA");
        }*/

        /*   }
        });*/

    } catch (Exception e) {
        // TODO: handle exception

        System.out.println("Greska PRILIKOM vracanja grafa:" + e.toString());
    }
}

From source file:com.conferenceengineer.android.iosched.gcm.command.SyncCommand.java

@Override
public void execute(Context context, String type, String extraData) {
    LOGI(TAG, "Received GCM message: " + type);
    int syncJitter;
    SyncData syncData = null;/*from w w  w. j a va  2  s.c  o m*/
    if (extraData != null) {
        try {
            JSONObject root = new JSONObject(extraData);
            int jitter = root.getInt("sync_jitter");
            syncData = new SyncData(jitter);
        } catch (JSONException e) {
            LOGI(TAG, "Error while decoding extraData: " + e.toString());
        }
    }

    if (syncData != null && syncData.sync_jitter != 0) {
        syncJitter = syncData.sync_jitter;
    } else {
        syncJitter = DEFAULT_TRIGGER_SYNC_MAX_JITTER_MILLIS;
    }

    scheduleSync(context, syncJitter);
}

From source file:co.mike.apptemplate.Utils.ServerUtils.RESTCient.java

public static String getRegisterIDFromJson(String json) {
    Log.e("RESPONSE :", json);
    String id = null;/* w w w .j  a  v  a  2s  .co  m*/
    JSONObject jsonObject;
    try {
        jsonObject = new JSONObject(json);
        id = jsonObject.getString("id");
    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
    }
    return id;
}

From source file:fr.haploid.webservices.WebServicesHelper.java

protected static synchronized WebServicesResponseData downloadFromServer(String url, JSONObject params,
        String type) {/*from w  w w . j a v  a2  s .  c  om*/

    int responseCode = 9999;

    StringBuffer buffer = new StringBuffer();

    Uri.Builder builder = new Uri.Builder();
    builder.encodedPath(url);

    JSONArray namesOfParams = params.names();

    for (int i = 0; i < namesOfParams.length(); i++) {
        try {
            String param = namesOfParams.getString(i);
            builder.appendQueryParameter(param, params.get(param).toString());
        } catch (JSONException e) {
            return new WebServicesResponseData("JSONException " + e.toString(), responseCode, false);
        }
    }

    URL urlBuild;

    try {
        urlBuild = new URL(builder.build().toString());
    } catch (MalformedURLException e) {
        return new WebServicesResponseData("MalformedURLException " + e.toString(), responseCode, false);
    }

    try {
        HttpURLConnection urlConnection = (HttpURLConnection) urlBuild.openConnection();
        try {
            urlConnection.setRequestMethod(type);
        } catch (ProtocolException e) {
            return new WebServicesResponseData("ProtocolException " + e.toString(), responseCode, false);
        }

        urlConnection.connect();
        responseCode = urlConnection.getResponseCode();
        if (responseCode < 400) {
            InputStream inputStream = urlConnection.getInputStream();
            if (inputStream == null) {
                return new WebServicesResponseData(null, responseCode, false);
            }
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

            String line;
            while ((line = reader.readLine()) != null) {
                buffer.append(line + "\n");
            }

            if (buffer.length() == 0) {
                return new WebServicesResponseData(null, responseCode, false);
            }
        }
    } catch (IOException e) {
        return new WebServicesResponseData("IOException " + e.toString(), responseCode, false);
    }
    return new WebServicesResponseData(buffer.toString(), responseCode, true);
}

From source file:com.krayzk9s.imgurholo.ui.SingleImageFragment.java

public void onGetObject(Object o, String tag) {
    if (getActivity() == null)
        return;/* www.  j  av a 2  s  .  c  o  m*/
    Log.d("tag", tag);
    if (tag.equals(REPLY)) {
        Toast.makeText(getActivity(), R.string.toast_explanation_comment_posted, Toast.LENGTH_SHORT).show();
        refreshComments();
    } else if (tag.equals(COMMENTS)) {
        JSONObject jsonObject = (JSONObject) o;
        if (jsonObject != null)
            commentData.setJSONObject(jsonObject);
        if (inGallery && commentAdapter != null) {
            addComments();
            commentAdapter.notifyDataSetChanged();
        }
        if (mPullToRefreshLayout != null)
            mPullToRefreshLayout.setRefreshComplete();
    } else if (tag.equals(DELETE)) {
        Toast.makeText(getActivity(), R.string.image_deleted, Toast.LENGTH_SHORT).show();
        getActivity().finish();
    } else if (tag.equals(POSTCOMMENT)) {
        Toast.makeText(getActivity(), R.string.toast_explanation_comment_posted, Toast.LENGTH_SHORT).show();
        refreshComments();
    } else if (tag.equals(GALLERY)) {
        JSONObject jsonObject = (JSONObject) o;
        try {
            if (jsonObject.getJSONObject("data").has("error")) {
                HashMap<String, Object> galleryMap = new HashMap<String, Object>();
                galleryMap.put("terms", "1");
                galleryMap.put(ImgurHoloActivity.IMAGE_DATA_TITLE, newGalleryString);
                Fetcher fetcher = new Fetcher(this, "3/gallery/" + imageData.getJSONObject().getString("id"),
                        ApiCall.POST, galleryMap, ((ImgurHoloActivity) getActivity()).getApiCall(),
                        GALLERYPOST);
                fetcher.execute();
                Toast.makeText(getActivity(), R.string.toast_explanation_comment_submitted, Toast.LENGTH_SHORT)
                        .show();
            } else {
                Fetcher fetcher = new Fetcher(this, "3/gallery/" + imageData.getJSONObject().getString("id"),
                        ApiCall.DELETE, null, ((ImgurHoloActivity) getActivity()).getApiCall(), GALLERYDELETE);
                fetcher.execute();
                Toast.makeText(getActivity(), R.string.toast_explanation_comment_removed, Toast.LENGTH_SHORT)
                        .show();
            }
        } catch (JSONException e) {
            Log.e("Error!", e.toString());
        }
    }
}

From source file:com.krayzk9s.imgurholo.ui.SingleImageFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    final ImgurHoloActivity activity = (ImgurHoloActivity) getActivity();
    final SharedPreferences settings = activity.getApiCall().settings;
    sort = settings.getString("CommentSort", "Best");
    boolean newData = true;
    if (commentData != null) {
        newData = false;/*  ww w .jav a2 s  . co  m*/
    }

    mainView = inflater.inflate(R.layout.single_image_layout, container, false);
    String[] mMenuList = getResources().getStringArray(R.array.emptyList);
    if (commentAdapter == null)
        commentAdapter = new CommentAdapter(mainView.getContext());
    commentLayout = (ListView) mainView.findViewById(R.id.comment_thread);
    commentLayout.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    if (settings.getString("theme", MainActivity.HOLO_LIGHT).equals(MainActivity.HOLO_LIGHT))
        imageLayoutView = (LinearLayout) View.inflate(getActivity(), R.layout.image_view, null);
    else
        imageLayoutView = (LinearLayout) View.inflate(getActivity(), R.layout.dark_image_view, null);

    mPullToRefreshLayout = (PullToRefreshLayout) mainView.findViewById(R.id.ptr_layout);
    ActionBarPullToRefresh.from(getActivity())
            // Mark All Children as pullable
            .allChildrenArePullable()
            // Set the OnRefreshListener
            .listener(this)
            // Finally commit the setup to our PullToRefreshLayout
            .setup(mPullToRefreshLayout);
    if (savedInstanceState != null && newData) {
        imageData = savedInstanceState.getParcelable("imageData");
        inGallery = savedInstanceState.getBoolean("inGallery");
    }
    LinearLayout layout = (LinearLayout) imageLayoutView.findViewById(R.id.image_buttons);
    TextView imageDetails = (TextView) imageLayoutView.findViewById(R.id.single_image_details);
    layout.setVisibility(View.VISIBLE);
    ImageButton imageFullscreen = (ImageButton) imageLayoutView.findViewById(R.id.fullscreen);
    imageUpvote = (ImageButton) imageLayoutView.findViewById(R.id.rating_good);
    imageDownvote = (ImageButton) imageLayoutView.findViewById(R.id.rating_bad);
    ImageButton imageFavorite = (ImageButton) imageLayoutView.findViewById(R.id.rating_favorite);
    imageComment = (ImageButton) imageLayoutView.findViewById(R.id.comment);
    ImageButton imageUser = (ImageButton) imageLayoutView.findViewById(R.id.user);
    imageScore = (TextView) imageLayoutView.findViewById(R.id.single_image_score);
    TextView imageInfo = (TextView) imageLayoutView.findViewById(R.id.single_image_info);
    Log.d("imageData", imageData.getJSONObject().toString());
    if (imageData.getJSONObject().has("ups")) {
        imageUpvote.setVisibility(View.VISIBLE);
        imageDownvote.setVisibility(View.VISIBLE);
        imageScore.setVisibility(View.VISIBLE);
        imageComment.setVisibility(View.VISIBLE);
        ImageUtils.updateImageFont(imageData, imageScore);
    }
    imageInfo.setVisibility(View.VISIBLE);
    ImageUtils.updateInfoFont(imageData, imageInfo);
    imageUser.setVisibility(View.VISIBLE);
    imageFavorite.setVisibility(View.VISIBLE);
    try {
        if (!imageData.getJSONObject().has("account_url")
                || imageData.getJSONObject().getString("account_url").equals("null")
                || imageData.getJSONObject().getString("account_url").equals("[deleted]"))
            imageUser.setVisibility(View.GONE);
        if (!imageData.getJSONObject().has("vote")) {
            imageUpvote.setVisibility(View.GONE);
            imageDownvote.setVisibility(View.GONE);
        } else {
            if (imageData.getJSONObject().getString("vote") != null
                    && imageData.getJSONObject().getString("vote").equals("up"))
                imageUpvote.setImageResource(R.drawable.green_rating_good);
            else if (imageData.getJSONObject().getString("vote") != null
                    && imageData.getJSONObject().getString("vote").equals("down"))
                imageDownvote.setImageResource(R.drawable.red_rating_bad);
        }
        if (imageData.getJSONObject().getString("favorite") != null
                && imageData.getJSONObject().getBoolean("favorite"))
            imageFavorite.setImageResource(R.drawable.green_rating_favorite);
    } catch (JSONException e) {
        Log.e("Error!", e.toString());
    }
    imageFavorite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ImageUtils.favoriteImage(singleImageFragment, imageData, (ImageButton) view, activity.getApiCall());
        }
    });
    imageUser.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ImageUtils.gotoUser(singleImageFragment, imageData);
        }
    });
    imageComment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Activity activity = getActivity();
            final EditText newBody = new EditText(activity);
            newBody.setHint(R.string.body_hint_body);
            newBody.setLines(3);
            final TextView characterCount = new TextView(activity);
            characterCount.setText("140");
            LinearLayout commentReplyLayout = new LinearLayout(activity);
            newBody.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                    //
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                    characterCount.setText(String.valueOf(140 - charSequence.length()));
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    for (int i = editable.length(); i > 0; i--) {
                        if (editable.subSequence(i - 1, i).toString().equals("\n"))
                            editable.replace(i - 1, i, "");
                    }
                }
            });
            commentReplyLayout.setOrientation(LinearLayout.VERTICAL);
            commentReplyLayout.addView(newBody);
            commentReplyLayout.addView(characterCount);
            new AlertDialog.Builder(activity).setTitle(R.string.dialog_comment_on_image_title)
                    .setView(commentReplyLayout)
                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            if (newBody.getText() != null && newBody.getText().toString().length() < 141) {
                                HashMap<String, Object> commentMap = new HashMap<String, Object>();
                                try {
                                    commentMap.put("comment", newBody.getText().toString());
                                    commentMap.put("image_id", imageData.getJSONObject().getString("id"));
                                    Fetcher fetcher = new Fetcher(singleImageFragment, "3/comment/",
                                            ApiCall.POST, commentMap,
                                            ((ImgurHoloActivity) getActivity()).getApiCall(), POSTCOMMENT);
                                    fetcher.execute();
                                } catch (JSONException e) {
                                    Log.e("Error!", e.toString());
                                }
                            }
                        }
                    }).setNegativeButton(R.string.dialog_answer_cancel, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            // Do nothing.
                        }
                    }).show();
        }
    });
    imageUpvote.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ImageUtils.upVote(singleImageFragment, imageData, imageUpvote, imageDownvote,
                    activity.getApiCall());
            ImageUtils.updateImageFont(imageData, imageScore);
        }
    });
    imageDownvote.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ImageUtils.downVote(singleImageFragment, imageData, imageUpvote, imageDownvote,
                    activity.getApiCall());
            ImageUtils.updateImageFont(imageData, imageScore);
        }
    });
    if (popupWindow != null) {
        popupWindow.dismiss();
    }
    popupWindow = new PopupWindow();
    imageFullscreen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ImageUtils.fullscreen(singleImageFragment, imageData, popupWindow, mainView);
        }
    });
    ArrayAdapter<String> tempAdapter = new ArrayAdapter<String>(mainView.getContext(),
            R.layout.drawer_list_item, mMenuList);
    Log.d("URI", "YO I'M IN YOUR SINGLE FRAGMENT gallery:" + inGallery);
    imageView = (ImageView) imageLayoutView.findViewById(R.id.single_image_view);
    loadImage();
    TextView imageTitle = (TextView) imageLayoutView.findViewById(R.id.single_image_title);
    TextView imageDescription = (TextView) imageLayoutView.findViewById(R.id.single_image_description);

    try {
        String size = String
                .valueOf(NumberFormat.getIntegerInstance()
                        .format(imageData.getJSONObject().getInt(ImgurHoloActivity.IMAGE_DATA_WIDTH)))
                + "x"
                + NumberFormat.getIntegerInstance()
                        .format(imageData.getJSONObject().getInt(ImgurHoloActivity.IMAGE_DATA_HEIGHT))
                + " (" + NumberFormat.getIntegerInstance()
                        .format(imageData.getJSONObject().getInt(ImgurHoloActivity.IMAGE_DATA_SIZE))
                + "B)";
        String initial = imageData.getJSONObject().getString(ImgurHoloActivity.IMAGE_DATA_TYPE) + " "
                + Html.fromHtml("&#8226;") + " " + size + " " + Html.fromHtml("&#8226;") + " " + "Views: "
                + NumberFormat.getIntegerInstance()
                        .format(imageData.getJSONObject().getInt(ImgurHoloActivity.IMAGE_DATA_VIEWS));
        imageDetails.setText(initial);
        Log.d("imagedata", imageData.getJSONObject().toString());
        if (!imageData.getJSONObject().getString(ImgurHoloActivity.IMAGE_DATA_TITLE).equals("null"))
            imageTitle.setText(imageData.getJSONObject().getString(ImgurHoloActivity.IMAGE_DATA_TITLE));
        else
            imageTitle.setVisibility(View.GONE);
        if (!imageData.getJSONObject().getString(ImgurHoloActivity.IMAGE_DATA_DESCRIPTION).equals("null")) {
            imageDescription
                    .setText(imageData.getJSONObject().getString(ImgurHoloActivity.IMAGE_DATA_DESCRIPTION));
            imageDescription.setVisibility(View.VISIBLE);
        } else
            imageDescription.setVisibility(View.GONE);
        commentLayout.addHeaderView(imageLayoutView);
        commentLayout.setAdapter(tempAdapter);
    } catch (JSONException e) {
        Log.e("Text Error!", e.toString());
    }
    if ((savedInstanceState == null || commentData == null) && newData) {
        commentData = new JSONParcelable();
        getComments();
        commentLayout.setAdapter(commentAdapter);
    } else if (newData) {
        commentArray = savedInstanceState.getParcelableArrayList("commentData");
        commentAdapter.addAll(commentArray);
        commentLayout.setAdapter(commentAdapter);
        commentAdapter.notifyDataSetChanged();
    } else if (commentArray != null) {
        commentAdapter.addAll(commentArray);
        commentLayout.setAdapter(commentAdapter);
        commentAdapter.notifyDataSetChanged();
    }
    return mainView;
}