6

Maybe the method is returning how it should, but I basically just made a test method that looks like this

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string TestJSON()
    {
        var location = new Location[2];
        location[0] = new Location();
        location[0].Latitute = "19";
        location[0].Longitude = "27";
        location[1] = new Location();
        location[1].Latitute = "-81.9";
        location[1].Longitude = "28";

        return new JavaScriptSerializer().Serialize(location);
    }

When i hit this from my android application I get an exception like this

Value <?xml of type java.lang.String cannot be converted to JSONArray

I thought this method would return just straight JSON, but this is what the web service method returns

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">[{"Latitute":"19","Longitude":"27"},{"Latitute":"-81.9","Longitude":"28"}]</string>

Is it suppose to be like this? Is there a way to remove the XML stuff that is outside the JSON? I'm not sure what I have to do in my webservice to make it return the correct format of data

Code Using on Android to Call the Webservice

   public String readWebService(String method)
{
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet("http://myserver.com/WebService.asmx/" + method);


    Log.d(main.class.toString(), "Created HttpGet Request object");

    try
    {
        HttpResponse response = client.execute(httpGet);
        Log.d(main.class.toString(), "Created HTTPResponse object");
        StatusLine statusLine = response.getStatusLine();
        Log.d(main.class.toString(), "Got Status Line");
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }

            return builder.toString();
        } else {
            Log.e(main.class.toString(), "Failed to contact Web Service: Status Code: " + statusCode);
        }
    }
    catch (ClientProtocolException e) {
        Log.e(main.class.toString(), "ClientProtocolException hit");
        e.printStackTrace();
    }
    catch (IOException e) {
        Log.e(main.class.toString(), "IOException hit");
        e.printStackTrace();
    }
    catch (Exception e) {
        Log.e(main.class.toString(), "General Exception hit");
    }

    return "WebService call failed";    
}

then I would call that method somewhere in the code like

try {
    JSONArray jsonArray = new JSONArray(readWebService("TestJSON"));
    Log.i(main.class.toString(), "Number of entries " + jsonArray.length());
        ....
}
...
3
  • How are you calling this from android? Are you specifying any contentype in that request? Commented Jul 1, 2012 at 18:31
  • I wasn't but I just tried adding httpGet.setHeader("Content-Type", "application/json"); when I add this the webservice returns a 500 server error status code. I'll update my question to include the android code I'm using to call the web service method Commented Jul 1, 2012 at 18:43
  • Looks like someone else was having a similar problem that I missed during my extensive research (5 min googling) stackoverflow.com/questions/2058454/… ...apparently it works if I use a POST instead of a get with the content type set to application/json Commented Jul 1, 2012 at 19:02

1 Answer 1

2

try this code.it will help you.

http://www.codeproject.com/Articles/45275/Create-a-JSON-WebService-in-ASP-NET-2-0-with-a-jQu

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.