0

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
esme_louise
  • 540
  • 2
  • 9
  • 28
killer
  • 592
  • 1
  • 9
  • 31
  • `Error` clearly states that you are trying to `converted String into JSONObject`. Debug your code and show the line where are you getting the `exception` – hrskrs May 12 '15 at 13:16
  • 1
    I agree with @trevor - try converting the 1st and last characters from `"` to `'`, because it seems that only the 1st part of the webservice data `string` is recognized.... Also, [this answer](http://stackoverflow.com/a/29053693/3372061) may help you. – Dev-iL May 12 '15 at 13:25
  • @Dev-iL thx needed to remove " character on start and end. – killer May 13 '15 at 05:39
  • does jsonFormattedString="[{"message":"Valid user!","status":true}]"...??? and one more thing...the object inside double quotes is JSONArray but not JSONObject – Angad Tiwari May 13 '15 at 05:57
  • and do not remove the backslashes '\'.. – Angad Tiwari May 13 '15 at 05:57

3 Answers3

1

Got it! Needed to Remove character " on start and end of json string like this:

  jsonFormattedString= jsonFormattedString.substring(1, jsonFormattedString.length()-1) ;

My array now became

 [{"message":"Valid user!","status":true}]
killer
  • 592
  • 1
  • 9
  • 31
1
"[{"message":"Valid user!","status":true}]"

If there exists double quotation at the beginning and end of the line then its not JSON data, it would a String that's why you are getting error. Correct JSON data would be [{"message":"Valid user!","status":true}] Then code like this,-

JSONArray array = new JSONArray(jsonString);
JSONObject object = array.getJSONObject(0);
Exigente05
  • 2,161
  • 3
  • 22
  • 42
  • Why will you use subString function where only need to fix the formation? It would be more flexible and easy way. – Exigente05 May 14 '15 at 12:37
0

you are casting JSONObject from a JSONArray.

JSONObject jsonmain = new JSONObject(jsonFormattedString); // jsonFormatteddString seems to be a JSONArray

try this :

JSONArray j = new JSONArray(jsonFormattedString);
JSONObject jsonmain = j.getJSONObject(0);

if you sometimes recieve JSONObject and sometimes JSONArray, you can try to change your code and check for what type you get.

A good example and explanation is at :

How to check whether the given object is object or Array in JSON string

Community
  • 1
  • 1
nafas
  • 5,283
  • 3
  • 29
  • 57