-1

I have successfully connected my android app to a C# WCF webservice using

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

This matches an HttpGet method on my WCF service. My question is now how do I adapt this code to communicate with a REST WebInvoke method in a C# webservice as below

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

How do I communicate with this method in android? Is there something built in like with HttpGet or do I need some sort of third party Android-REST library?

NZJames
  • 4,963
  • 15
  • 50
  • 100

1 Answers1

0

First, in Android cant do network tasks in the main thread, you must do it async Create this Class

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

        }

This code for process the response

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

For use it sample

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();
        }
    }
FRL
  • 748
  • 7
  • 9