我的网址是 http://example.com/WcfService/MasterService.svc
方法名称是“SearchOrganizations”;
字符串网址=网址1 +“/”+ 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;字符集= UTF-8
我怎么能设法调用POST服务。 我已尝试使用不同的方法来调用此服务但未能获得响应
http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/
但我无法得到答复。
答案 0 :(得分:2)
尝试使用下面的代码片段,它在WCF情况下工作正常。将您的参数以namevalue对传递给方法,您将从方法获得字符串响应。是的,在测试此片段之前检查所有permisions,因为您需要添加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;
}
}
答案 1 :(得分:-1)
我的代码也运行正常但是网络服务方面的网址存在问题。
http://example.com/WcfService/MasterService.svc/web/SearchOrganizations/
感谢您帮助我用户1621629 ......