String value = myInput.getText().toString();
I want to put string value into JSONObject.put("STRING",value) method but it's not working..
See the attached screen shot and tell how to resolve this.
String value = myInput.getText().toString();
I want to put string value into JSONObject.put("STRING",value) method but it's not working..
See the attached screen shot and tell how to resolve this.
Add try.. catch
String data = "";
String val = "hello";
try {
JSONObject j = new JSONObject(data);
j.put("VAL", val);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You should wrap the code inside try-catch
try {
JSONObject j = new JSONObject(data);
} catch (JSONException e) {
e.printStackTrace();
}
Why try-catch is compulsary??
There are 2 kinds of exceptions: Checked and Unchecked.
Checked exception can be considered one that is found by the compiler, and the compiler knows that it has a chance to occur, so you need to catch or throw it.
Unchecked exception is a Runtime Exception which means these are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from.
JSONException is a type of Checked exception. Checked exceptions needs to be handled at compile time itself.
new JSONObject(data) will throw JSONException if the parse fails or doesn't yield a JSONObject. So it is recommended to wrap it inside a try-catch block at compile time itself & the underlying IDE will show an error message for the same.