Erdinç Özdemir
Erdinç Özdemir

Reputation: 1381

Getting "Invalid JSON primitive" error while trying to get data from WebService

I'm working on an android app and I want to get some data from a WebService. I'm using this code to get JSON data from the WebService.

    TextView textv=(TextView) findViewById(R.id.textv);

    try {
        HttpClient client = new DefaultHttpClient();  
        String URL = "http://server/WebService.asmx/Get_ActiveFair";
        HttpPost post = new HttpPost(URL);

        post.setHeader("Content-Type", "application/json; charset=utf-8");
        HttpResponse responsePost = client.execute(post);
        HttpEntity resEntityPost = responsePost.getEntity();  
        if (resEntityPost != null) 
        {
            String response=EntityUtils.toString(resEntityPost);
            Log.e("XXX",response);
            textv.setText(response);
        }
    } catch (Exception e) {
        e.printStackTrace();
        textv.setText(e.toString());
        Log.e("error!!",e.toString());
    }

It works correctly an I get the data like this:

     {
        "d": "{\"Id\":2,\"Name\":\"Fair Name \",\"IsActive\":true,\"Date_Start\":\"\\/Date(1383343200000)\\/\",\"Date_End\":\"\\/Date(1384034400000)\\/\",\"Url_Map\":null,\"Details\":\"Fair Details \",\"Address\":\"FairAdress \",\"VisitingInfo\":\"Fair Visiting Info\",\"Contact\":null,\"Transportation\":\" Fair Transportation Info \"}"
     }

But when I want to use another method in the webservice which needs to get FairId I get the result:

     {
        "Message": "Invalid JSON primitive: FairId.",
        "StackTrace": "   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)",
        "ExceptionType": "System.ArgumentException"
     }

And here is my code to run the Get_EventList method:

    TextView textv=(TextView) findViewById(R.id.textv);

    try {
        HttpClient client = new DefaultHttpClient();  
        String URL = "http://server/WebService.asmx/Get_EventList";
        HttpPost post = new HttpPost(URL);

        List<NameValuePair> postParameters;
        postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("FairId", "2"));
        post.setEntity(new UrlEncodedFormEntity(postParameters));

        post.setHeader("Content-Type", "application/json; charset=utf-8");
        HttpResponse responsePost = client.execute(post);
        HttpEntity resEntityPost = responsePost.getEntity();  
        if (resEntityPost != null) 
        {
            String response=EntityUtils.toString(resEntityPost);
            Log.e("XXX",response);
            textv.setText(response);
        }
    } catch (Exception e) {
        e.printStackTrace();
        textv.setText(e.toString());
        Log.e("hata!!",e.toString());
    }

What can be the problem? How can I solve it?

Upvotes: 1

Views: 1483

Answers (1)

Erdin&#231; &#214;zdemir
Erdin&#231; &#214;zdemir

Reputation: 1381

I solve the problem by sending FairId to WebService with JSONObject. Here is my new code:

    TextView textv=(TextView) findViewById(R.id.textv);

    try {
        HttpClient client = new DefaultHttpClient();  
        String URL = "http://server/WebService.asmx/Get_EventList";
        HttpPost post = new HttpPost(URL);


        JSONObject json = new JSONObject();
        json.put("FairId", "2");
        StringEntity se = new StringEntity( json.toString());  
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        post.setEntity(se);

        HttpResponse responsePost = client.execute(post);
        HttpEntity resEntityPost = responsePost.getEntity();  
        if (resEntityPost != null) 
        {
            String response=EntityUtils.toString(resEntityPost);
            Log.e("XXX",response);
            textv.setText(response);
        }
    } catch (Exception e) {
        e.printStackTrace();
        textv.setText(e.toString());
        Log.e("hata!!",e.toString());
    }

Upvotes: 1

Related Questions