我的網址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/
但我可能無法得到響應。
沒有得到來自服務的響應.. – 2014-09-24 09:41:04
請與鍍鉻後的客戶端web服務的。在鉻郵遞員 – user1621629 2014-09-24 09:56:50
我得到了同樣的答覆,我在我的問題得到。但是使用你的代碼我無法得到任何迴應 – 2014-09-25 05:38:49