【问题标题】:calling the wcf service POST from the android从 android 调用 wcf 服务 POST
【发布时间】:2014-09-24 08:33:42
【问题描述】:

我的网址是 http://example.com/WcfService/MasterService.svc

方法名称是“SearchOrganizations”;

字符串 url = URL1 + "/" + METHOD_NAME_SEARCH_ORGANIZATION + "/";

所以我的网址将是 http://example.com/WcfService/MasterService.svc/SearchOrganizations/

服务类型是 POST。这是我从 android 调用此服务的代码

JSONObject jsonObjSend = new JSONObject();
        try {
            jsonObjSend.put("data", params[0]);
            jsonObjSend.put("CurrentUserId", params[1]);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        List<NameValuePair> paramswithkey = new ArrayList<NameValuePair>();
        paramswithkey.add(new BasicNameValuePair("data", params[0]));
        paramswithkey
                .add(new BasicNameValuePair("CurrentUserId", params[1]));
        HttpClient client = new DefaultHttpClient();
        HttpPost httpPostRequest = new HttpPost(url);

        // httpPostRequest.setHeader("Accept", "application/soap+xml");
        httpPostRequest.setHeader("Content-Type",
                "application/x-www-form-urlencoded; charset=utf-8");
        UrlEncodedFormEntity se = null;
        try {
            // se = new StringEntity(jsonObjSend.toString());
            se = new UrlEncodedFormEntity(/*
                                         * jsonObjSend.toString().getBytes(
                                         * "UTF8")
                                         */paramswithkey);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        httpPostRequest.setEntity(se);
        HttpResponse response = null;
        try {
            response = client.execute(httpPostRequest);
        } catch (ClientProtocolException e) {
            Toast.makeText(getApplicationContext(),
                    "Please check your internet connection",
                    Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        logI("BEFOR response METHOD_NAME_SEARCH_ORGANIZATION :" + response);
        BasicResponseHandler responseHandler = new BasicResponseHandler();
        String strResponse = null;
        if (response != null) {
            try {
                strResponse = responseHandler.handleResponse(response);
            } catch (HttpResponseException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        logI("AFTER response METHOD_NAME_SEARCH_ORGANIZATION :"
                + strResponse);

我已经通过 JsonObject 和 NameValuePair 使用了这段代码,但我得到了以下异常

 org.apache.http.client.HttpResponseException: Cannot process the message because the content type 'application/x-www-form-urlencoded; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'.

.Net 方面,他们使用 x-www-form-urlencoded 调用了服务,但我得到的预期类型应该是 text/xml; charset=utf-8

我怎么能设法调用 POST 服务。 我尝试了不同的方式来调用此服务,但未能得到响应

http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/

但我无法得到响应。

【问题讨论】:

    标签: android web-services wcf-data-services


    【解决方案1】:

    尝试使用下面的代码片段,它在 WCF 案例中可以正常工作。将名称值对中的参数传递给方法,您将从方法中获得字符串响应。是的,在测试此片段之前检查所有权限,因为您需要添加 intetn

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("username", name));
    
    public static String postRequest(String url, List<NameValuePair> nameValuePairs) {
            String getServerPath = Utils.SERVER_ADDRESS + "methodname";
            String result = null;
    
            try {
                Log.e("TAG", "url:: " + url);
                JSONObject jsonObject = new JSONObject();
    
                for (NameValuePair nvp : nameValuePairs) {
                    String name = nvp.getName();
                    String value = nvp.getValue();
                    jsonObject.accumulate(name, value);
                    Log.e("TAG", name + "=" + value);
                }
                HttpClient HC = new DefaultHttpClient();
                HttpPost post = new HttpPost(url);
                StringEntity se = new StringEntity(jsonObject.toString());
                se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                post.setEntity(se);
                HttpResponse res = HC.execute(post);
                HttpEntity entity = res.getEntity();
                result = EntityUtils.toString(entity);
                Log.e("TAG", "result:: " + result);
                return result;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
    
        }
    

    【讨论】:

    • 使用 chrome post 客户端检查您的网络服务。
    • 在 chrome postman 中,我得到了与我的问题相同的回复。但是使用您的代码我无法得到任何响应
    • 哟。您需要咨询您的 Web 服务开发人员。由于来自 Android 的代码完美运行。
    【解决方案2】:

    我的代码也可以正常工作,但是给我的 web 服务端的 url 存在问题。

    http://example.com/WcfService/MasterService.svc/web/SearchOrganizations/
    

    感谢您帮助我 user1621629...

    【讨论】:

      猜你喜欢
      • 2023-04-10
      • 1970-01-01
      • 2012-03-05
      • 2012-03-05
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多