1

我有一个 wcf 服务,其中包含一些我将发布的方法,例如:

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

班上

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

在活动中

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();
        }

我每次都为空。如果我从浏览器或模拟器访问 URL,我会得到:["ag","asda"]。

4

1 回答 1

0

嗨试试下面的代码

你可以参考这里的教程

    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);
于 2013-03-01T10:21:06.527 回答