2014-09-24 80 views
1

我的網址WCF服務POST是 http://example.com/WcfService/MasterService.svc調用從Android

方法名是 「SearchOrganizations」;

String url = URL1 +「/」+ METHOD_NAME_SEARCH_ORGANIZATION +「/」;

所以我的URL將被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'. 

淨側的例外,他們都呼籲該服務使用x-www-form-urlencoded但我得到的預期類型應該是text/xml; charset = utf-8

我怎樣才能設法調用POST服務。 我曾嘗試用不同的方式來調用這個服務,但未能得到響應

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

但我可能無法得到響應。

回答

-1

我的代碼也工作正常,但有從已給我的web服務端URL的問題。

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

謝謝你幫我user1621629 ...

2

嘗試使用下面的代碼snipt它在WCF情況下工作perfact。將名稱值對中的參數傳遞給方法,您將從方法獲得字符串響應。是的測試之前檢查所有permisions這snipt,因爲你需要添加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; 
     } 

    } 
+0

沒有得到來自服務的響應.. – 2014-09-24 09:41:04

+0

請與鍍鉻後的客戶端web服務的。在鉻郵遞員 – user1621629 2014-09-24 09:56:50

+0

我得到了同樣的答覆,我在我的問題得到。但是使用你的代碼我無法得到任何迴應 – 2014-09-25 05:38:49