[英]calling the wcf service POST from the android
我的网址是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; 字符集= utf-8的
我如何设法致电POST服务。 我尝试了不同的方式来调用此服务,但未能获得响应
http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/
但我无法得到回应。
尝试使用下面的代码片段,在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;
}
}
我的代码也可以正常工作,但是从Webservice端收到的网址存在问题。
http://example.com/WcfService/MasterService.svc/web/SearchOrganizations/
感谢您帮助我user1621629 ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.