如何从Android应用程序中与c# REST WebInvoke方法通信

本文关键字:REST WebInvoke 方法 通信 Android 应用程序 | 更新日期: 2023-09-27 18:06:09

我已经成功地使用

将我的android应用程序连接到一个c# WCF webservice。
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(SERVICE_URI);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
HttpResponse response = client.execute(request);

这与我的WCF服务上的HttpGet方法匹配。我的问题是现在我如何调整这段代码与c# web服务中的REST WebInvoke方法进行通信,如下所示

[OperationContract]
        [WebInvoke(
            Method = "POST",
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json,
            UriTemplate = "/GetItems"
            )]
        GetItemsResponse GetItems(GetItemsRequest request);

如何在android中与此方法通信?是否有内置的东西,如HttpGet或我需要某种第三方Android-REST库?

如何从Android应用程序中与c# REST WebInvoke方法通信

首先,在Android中不能在主线程中执行网络任务,必须异步执行创建这个类

    private class SomeServiceRequest extends AsyncTask<JSONObject, Long, JSONObject> {
            @Override
            protected JSONObject doInBackground(JSONObject... params) {
                HttpPost request = new HttpPost(SERVICE_URI);
                request.setHeader("Accept", "application/json; charset=utf-8");
                request.setHeader("Content-type", "application/json; charset=utf-8");
                JSONObject result = null;
                // Build JSON string
                try {
                    StringEntity entity = new StringEntity(params[0].toString(),
                            "UTF-8");
                    request.setEntity(entity);
                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    HttpResponse response = httpClient.execute(request);
                    StatusLine sl = response.getStatusLine();
                    if (sl.getStatusCode() == 200) {
                        result = new JSONObject(EntityUtils.toString(response
                                .getEntity()));
                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return result;
            }
            @Override
            protected void onPostExecute(JSONObject result) {
   processSvcResult(result);
                super.onPostExecute(result);
            }
        }

处理响应

的代码
private void processSvcResult(JSONObject result) {
        if (result != null) {
            try {
                Boolean success = result.getString("Status").equals("OK");
                storeRegistrationSuccess(context, success);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

使用示例

private void CallSomeService(String messageId) {
        final SharedPreferences prefs = getSharedPreferences(
                this.getClass().getSimpleName(), Context.MODE_PRIVATE);
        // PROPERTY_IDDEVICE
        String iddevice = prefs.getString(PROPERTY_IDDEVICE, "");
        JSONObject p = new JSONObject();
        try {
            p.put("IDDevice", iddevice.toLowerCase(Locale.ROOT));
            p.put("MessageID", messageId);
            new SomeServiceRequest().execute(p);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }