Android从WCF检索返回值
我有一个Android应用程序,可以通过下面的代码发布到web服务,一切正常。但是服务中的myContract方法返回布尔值,true或false。我如何检索该值,以便在错误时告诉我的应用程序是否继续Android从WCF检索返回值,android,wcf,Android,Wcf,我有一个Android应用程序,可以通过下面的代码发布到web服务,一切正常。但是服务中的myContract方法返回布尔值,true或false。我如何检索该值,以便在错误时告诉我的应用程序是否继续 HttpPost request = new HttpPost(SERVICE_URI + "/myContract/someString"); request.setHeader("Accept", "application/json"); request.setHeader("Content
HttpPost request = new HttpPost(SERVICE_URI + "/myContract/someString");
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
编辑
很抱歉进行了编辑,但是使用HttpResponse,然后使用logging或toasting response.toString()返回一个我不理解的字符串
更新
谢谢谢里夫
但这似乎有点太多的信息和代码来做我想做的事情。我在下面添加了一些有效的代码,但我不确定它是否正确。该服务将返回一个布尔值true或false,以判断帖子是否成功,但我似乎正在以字符串的形式检索它
HttpEntity responseEntity = response.getEntity();
char[] buffer = new char[(int)responseEntity.getContentLength()];
InputStream stream = responseEntity.getContent();
InputStreamReader reader = new InputStreamReader(stream);
reader.read(buffer);
stream.close();
JSONObject jsonResponse = new JSONObject(new String(buffer));
String ServiceResponse = jsonResponse.getString("putCommuniqueResult");
Log.d("WebInvoke", "Saving : " + ServiceResponse);
这样行吗?它是有效的,但我不确定它是否正确!
干杯
迈克
注意:在这种情况下,如果只检查布尔值,实际上不需要解析JSON或XML,那么您就知道如果您发现了true,那么它就是true,如果发现了其他内容,那么它就是false
如果您需要使用XML或JSON获取数据,您可以参考此答案Web服务设计为能够与所有编程语言通信,除非您使用相同的技术读取(.NET),否则您将始终以字符串形式读取数据,并将其解析为所需的目标格式,也就是说,C#可以添加web服务引用,您将以您需要的格式获取数据,但使用任何其他语言,除非您获得一个解析wsdl的库,然后将wcf字符串解析为实际输出,否则在解析它之前,它将始终是字符串
private static String getDataFromXML(final String text) {
final String temp = new String(text).split("<")[2].split(">")[1];
final String temp2 = temp.replace("<", "<").replace(">", ">")
.replace("&", "&");
return temp2;
}
/**
* Connects to the web service and returns the pure string returned, NOTE:
* if the generated url is more than 1024 it automatically delegates to
* connectPOST
*
* @param hostName
* : the host name ex: google.com or IP ex:
* 127.0.0.1
* @param webService
* : web service name ex: TestWS
* @param classOrEndPoint
* : file or end point ex: CTest
* @param method
* : method being called ex: TestMethod
* @param parameters
* : Array of {String Key, String Value} ex: { { "Username",
* "admin" }, { "Password", "313233" } }
* @return the trimmed String received from the web service
*
* @author Shereef Marzouk - http://shereef.net
*
*
*/
public static String connectGET(final String hostNameOrIP,
final String webService, final String classOrEndPoint,
final String method, final String[][] parameters) {
String url = "http://" + hostNameOrIP + "/" + webService + "/"
+ classOrEndPoint + "/" + method;
String params = "";
if (null != parameters) {
for (final String[] strings : parameters) {
if (strings.length == 2) {
if (params.length() != 0) {
params += "&";
}
params += strings[0] + "=" + strings[1];
} else {
Log.e(Standards.TAG,
"The array 'parameters' has the wrong dimensions("
+ strings.length + ") in " + method + "("
+ parameters.toString() + ")");
}
}
}
url += "?" + params;
if (url.length() >= 1024) { // The URL will be truncated if it is more
// than 1024
return Communications.connectPOST(hostNameOrIP, webService,
classOrEndPoint, method, parameters);
}
final StringBuffer text = new StringBuffer();
HttpURLConnection conn = null;
InputStreamReader in = null;
BufferedReader buff = null;
try {
final URL page = new URL(url);
conn = (HttpURLConnection) page.openConnection();
conn.connect();
in = new InputStreamReader((InputStream) conn.getContent());
buff = new BufferedReader(in);
String line;
while (null != (line = buff.readLine()) && !"null".equals(line)) {
text.append(line + "\n");
}
} catch (final Exception e) {
Log.e(Standards.TAG,
"Exception while getting " + method + " from " + webService
+ "/" + classOrEndPoint + " with parameters: "
+ params + ", exception: " + e.toString()
+ ", cause: " + e.getCause() + ", message: "
+ e.getMessage());
Standards.stackTracePrint(e.getStackTrace(), method);
return null;
} finally {
if (null != buff) {
try {
buff.close();
} catch (final IOException e1) {
}
buff = null;
}
if (null != in) {
try {
in.close();
} catch (final IOException e1) {
}
in = null;
}
if (null != conn) {
conn.disconnect();
conn = null;
}
}
if (text.length() > 0 && Communications.checkText(text.toString())) {
final String temp = Communications.getDataFromXML(text.toString());
Log.i(Standards.TAG, "Success in " + method + "(" + params
+ ") = " + temp);
return temp;
}
Log.w(Standards.TAG, "Warning: " + method + "(" + params + "), text = "
+ text.toString());
return null;
}
public boolean isWSTrue() {
String data = connectGET("google.com",
"wcfsvc", "service.svc",
"showuserdata/11949", null);
if(null != data && data.length() >0)
return data.toLowerCase().contains("true");
throw new Exception("failed to get webservice data");
}