【发布时间】: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