1

I have a wcf service with some methods I will post one for example:

[OperationContract]
    [WebGet(
        UriTemplate = "GetPlates",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)]
    IList<string> GetPlates();

the class

   public IList<string> GetPlates()
    {
        return new List<string>
                   {
                       "ag",
                       "asda"
                   };
    }

in the activity

try {


             // Send GET request to <service>/GetPlates
            HttpGet request = new HttpGet(SERVICE_URI + "/GetPlates");
            request.setHeader("Accept", "application/json");
            request.setHeader("Content-type", "application/json");

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = httpClient.execute(request);

            HttpEntity responseEntity = response.getEntity();

            // Read response data into buffer
            char[] buffer = new char[(int)responseEntity.getContentLength()];
            InputStream stream = responseEntity.getContent();
            InputStreamReader reader = new InputStreamReader(stream);
            reader.read(buffer);
            stream.close();

            JSONArray plates = new JSONArray(new String(buffer));


        } catch (Exception e) {

            e.printStackTrace();
        }

I get null everyttime. If I acces the URL from my browser or from the emulator I get: ["ag","asda"].

Robert P
  • 137
  • 2
  • 10
  • if its working on emulator but not on phone then first check if the ip to server is accessible from your phone – Waqas Mar 01 '13 at 10:22
  • If you're not getting an exception, then you can check your DNS. Try connecting to your local network via wifi and see if that eliminates the problem. – androidgeek Mar 01 '13 at 10:26

1 Answers1

0

Hi try the following code

You can refer the tutorial here

    class RetreiveFeedTask extends AsyncTask<String, Void, RSSFeed> {

    private Exception exception;

    protected RSSFeed doInBackground(String... urls) {
    try {


         // Send GET request to <service>/GetPlates
        HttpGet request = new HttpGet(SERVICE_URI + "/GetPlates");
        request.setHeader("Accept", "application/json");
        request.setHeader("Content-type", "application/json");

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(request);

        HttpEntity responseEntity = response.getEntity();

        // Read response data into buffer
        char[] buffer = new char[(int)responseEntity.getContentLength()];
        InputStream stream = responseEntity.getContent();
        InputStreamReader reader = new InputStreamReader(stream);
        reader.read(buffer);
        stream.close();

        JSONArray plates = new JSONArray(new String(buffer));


    } catch (Exception e) {

        e.printStackTrace();
    }

   protected void onPostExecute(RSSFeed feed) {
     // TODO: check this.exception 
    // TODO: do something with the feed
    }
  }

  new RetreiveFeedTask().execute(urlToRssFeed);
Community
  • 1
  • 1
androidgeek
  • 3,440
  • 1
  • 15
  • 27
  • Have you given Internet Permissions in AndroidMainfest file check once if not please provide Internet Permissions ` ` – androidgeek Mar 01 '13 at 10:34
  • Now check the above code that i have modified your code lets try this code .... or otherwise refer the post with the same issue... – androidgeek Mar 01 '13 at 10:40
  • Actually this exception is thrown when an application attempts to perform a networking operation on its main thread. Run your code in AsyncTask: please verify this link `http://developer.android.com/reference/android/os/AsyncTask.html` – androidgeek Mar 01 '13 at 10:56
  • I've done this but still no results. I don't get the resul I don't understand what is happening. – Robert P Mar 01 '13 at 12:13