I have a json wcf service which I am trying to access on my localhost emulator. Service is accessed successfully but I am getting strange exception in my code. When I try to convert json string to json object I get the exception in logcat.
Value [{ of type java.lang.String cannot be converted to JSONObject
Data I am getting from webservice is like this:
"[{"message":"Valid user!","status":true}]"
My code is like this:
protected Boolean doInBackground(String... params) {
String line = "";
String ur = "http://10.0.2.2:28088/HighriseeSite/appservices.svc/login?username="+etUserName.getText().toString()+"&pass="+etPassword.getText().toString();
Log.d("STRIMGuuuu",ur);
try {
// Replace it with your own WCF service path
URL json = new URL(ur);
URLConnection jc = json.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(jc.getInputStream()));
line = reader.readLine();
String jsonFormattedString = line.replaceAll("\\\\", "");
Log.d("Json String--->",jsonFormattedString);
JSONObject jsonmain = new JSONObject(jsonFormattedString);
if(jsonmain.getBoolean("status")) {
sharPref = getSharedPreferences("LoginInfo", MODE_PRIVATE);//mode private means that this logininfo cannot be accessed by other apps
SharedPreferences.Editor editor = sharPref.edit();
editor.putString("UserName", "sitemanager");
editor.putString("Password", "admin123$");
editor.putBoolean("Login", true);
editor.commit();
}
return jsonmain.getBoolean("status");
}
catch(Exception e) {
Log.d("Error--->",e.getMessage());
return false;
}
}
Logs from logcat are like this:
05-12 18:33:22.336 1874-1900/kanix.highrise.com.highrise D/Json String--->﹕ "[{"message":"Valid user!","status":true}]"
05-12 18:33:22.347 1874-1900/kanix.highrise.com.highrise D/Error--->﹕ Value [{ of type java.lang.String cannot be converted to JSONObject
05-12 18:33:22.354 1874-1895/kanix.highrise.com.highrise W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-12 18:33:22.354 1874-1895/kanix.highrise.com.highrise W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa591ee40, error=EGL_SUCCESS
05-12 18:33:22.383 1874-1886/kanix.highrise.com.highrise I/art﹕ Background sticky concurrent mark sweep GC freed 4572(191KB) AllocSpace objects, 0(0B) LOS objects, 11% free, 1669KB/1884KB, paused 1.143ms total 139.646ms
05-12 18:33:22.706 1874-1895/kanix.highrise.com.highrise W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-12 18:33:22.706 1874-1895/kanix.highrise.com.highrise W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa591ee40, error=EGL_SUCCESS
Errorclearly states that you are trying toconverted String into JSONObject. Debug your code and show the line where are you getting theexception"to', because it seems that only the 1st part of the webservice datastringis recognized.... Also, this answer may help you.