My url is: http://a.nextput.com/apps/init/4/a/9fe2d2cbaa8332a4633be17b79208181-2y-10-ELVM4HwkaYaCVu6203Zjfus-G/o?aff_id={aff_id}
It contains single object {"success":true}. How to parse this url and store the json data in a variable?
my doInBackground method:
protected Void doInBackground(Void... unused) {
String json = "";
URL url;
HttpURLConnection connection = null;
try {
url = new URL("http://a.nextput.com/apps/init/4/a/9fe2d2cbaa8332a4633be17b79208181-2y-10-ELVM4HwkaYaCVu6203Zjfus-G/o?aff_id=");
connection = (HttpURLConnection) url.openConnection();
InputStream inputStream = connection.getInputStream();
InputStreamReader reader = new InputStreamReader(inputStream);
int data = reader.read();
while (data != -1) {
char currentChar = (char) data;
data = reader.read();
json += currentChar;
}
}catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return json;
JSONObject jsonObject = new JSONObject(json);
boolean state = jsonObject.getBoolean("success");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("state",state);
editor.commit();
return null;
}
In return json; it is showing incompatible types and in JSONObject it is showing unhandled exception: org.json.JSONException. How to resolve it?