Android调用连接.net写的java调用webservicee返回Datetable返回值

一 、服务器端设置
&&& 1.修改web.config
在web.config里面的的system.Web节点添加
&&&& &webServices&&
&&&&&& &protocols&&
&&&&&&&& &add name= "HttpPost"/&&
&&&&&&&& &add name= "HttpGet"/&&
&&&&&& &/protocols&&
&&&& &/webServices&&
&&&&&&& &&&&&&& &&&&& &&&
2.修改XX.asmx.cs
& [WebService(Namespace = "")] &&& [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] &&& [System.ComponentModel.ToolboxItem(false)] &&& [System.Web.Script.Services.ScriptService]
//至此,一般函数可在android中可以通过代码返回json数据,返回datatable的函数不可以
3.返回datatable的函数修改
此处需要用到一个函数将datatable转换成json格式
&#region dataTable转换成Json格式 &&&&&&& /// &&&& &&&&&&& /// dataTable转换成Json格式&&&& &&&&&&& /// &&&& &&&&&&& /// &&&& &&&&&&& /// &&&& &&&&&&& public string DataTableJson(DataTable dt) &&&&&&& { &&&&&&&&&&& StringBuilder jsonBuilder = new StringBuilder(); &&&&&&&&&&& jsonBuilder.Append("{""); &&&&&&&&&&& jsonBuilder.Append(dt.TableName.ToString()); &&&&&&&&&&& jsonBuilder.Append("":["); &&&&&&&&&&& for (int i = 0; i & dt.Rows.C i++) &&&&&&&&&&& { &&&&&&&&&&&&&&& jsonBuilder.Append("{"); &&&&&&&&&&&&&&& for (int j = 0; j & dt.Columns.C j++) &&&&&&&&&&&&&&& { &&&&&&&&&&&&&&&&&&& jsonBuilder.Append("""); &&&&&&&&&&&&&&&&&&& jsonBuilder.Append(dt.Columns[j].ColumnName); &&&&&&&&&&&&&&&&&&& jsonBuilder.Append("":""); &&&&&&&&&&&&&&&&&&& jsonBuilder.Append(dt.Rows[i][j].ToString()); &&&&&&&&&&&&&&&&&&& jsonBuilder.Append("","); &&&&&&&&&&&&&&& } &&&&&&&&&&&&&&& jsonBuilder.Remove(jsonBuilder.Length - 1, 1); &&&&&&&&&&&&&&& jsonBuilder.Append("},"); &&&&&&&&&&& } &&&&&&&&&&& jsonBuilder.Remove(jsonBuilder.Length - 1, 1); &&&&&&&&&&& jsonBuilder.Append("]"); &&&&&&&&&&& jsonBuilder.Append("}"); &&&&&&&&&&& return jsonBuilder.ToString(); &&&&&&& }
& #endregion
再将原本需要返回datatable的函数新建一个类似函数。此处注意,一定要写
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)],不然android在获取数据时系统又会将转换好的json数据再加一层json。 比如
&[WebMethod] &&&&&&& [ScriptMethod(ResponseFormat = ResponseFormat.Xml)] &&&&&&& public string HPReadQueryPhone(string strSql, string BD) &&&&&&& { &&&&&&&&&&& try &&&&&&&&&&& { &&&&&&&&&&&&&&& MSQuery dbTable = new MSQuery(); &&&&&&&&&&&&&&& DataSet dsTable = dbTable.HPReadQuery(strSql, BD); &&&&&&&&&&&&&&& dbTable = &&&&&&&&&&&&&&& if (dsTable.Tables.Count == 0) &&&&&&&&&&&&&&& { &&&&&&&&&&&&&&&&&&& return ""; &&&&&&&&&&&&&&& } &&&&&&&&&&&&&&& else &&&&&&&&&&&&&&& { &&&&&&&&&&&&&&&&&&& return DataTableJson(dsTable.Tables[0]); &&&&&&&&&&&&&&& } &&&&&&&&&&& } &&&&&&&&&&& catch (System.Exception err) &&&&&&&&&&& { &&&&&&&&&&&&&&& ThrowException(err); &&&&&&&&&&&&&&& &&&&&&&&&&& } &&&&&&& }
二、android代码写法
try{ &&String SERVER_URL = ""; && HttpPost request = new HttpPost(SERVER_URL); // 根据内容来源地址创建一个Http请求& & &&&&&&&& request.addHeader("Content-Type", "application/ charset=utf-8");//必须要添加该Http头才能调用WebMethod时返回JSON数据&&&&&&&&&&&& &&&&&&&& JSONObject jsonParams=new JSONObject(); //&&&&&&&& jsonParams.put("strdate", "1"); //&&&&&&&& jsonParams.put("aa", "1"); &&&&&&&& jsonParams.put("strSql", "select * from RRUser");//传参,如果想传递两个参数则继续添加第二个参数jsonParams.put("param2Name","param2Value") &&&&&&&& jsonParams.put("BD", "27"); &&&&&&&& HttpEntity bodyEntity =new StringEntity(jsonParams.toString(), "utf8");//参数必须也得是JSON数据格式的字符串才能传递到服务器端,否则会出现"{'Message':'strUserName是无效的JSON基元'}"的错误 &&&&&&&& Log.i("ex",jsonParams.toString()); &&&&&&&& request.setEntity(bodyEntity); &&&&&&&& HttpResponse httpResponse = new DefaultHttpClient().execute(request); // 发送请求并获取反馈 &&&&&&&& ShowMessage(httpResponse.getStatusLine().toString()); &&&&&&&& if(httpResponse.getStatusLine().getStatusCode()==200) &&&&&&&& { &&&&&&& & &&&&&&&& String result = EntityUtils.toString(httpResponse.getEntity()); &&&&&&&& Log.i("ex", result); &&&&&&&& ShowMessage(result); &&&&&&&& if(!result.equals("")) &&&&&&&& { &&&&&&& & try &&&&&&& & { &&&&&&& && JSONArray&& jsonObjs = new JSONObject (result).getJSONArray("Table");&& &&&&&&& && JSONObject jsonObj = ((JSONObject)jsonObjs.opt(0)); &&&&&&& &&&&&& String username=jsonObj.getString("UserCname"); &&&&&&& &&&&&& ShowMessage(username);
&&&&&& && Log.i("ex",String.valueOf(jsonObjs.length())+"aa");
&&&&&&& & } &&&&&&& & catch(JSONException e) &&&&&&& & { &&&&&&& && ShowMessage("数据错误"); &&&&&&& & } &&&&&&&& } &&&&&& &&&&&&&& } &&&&&&&& else &&&&&&&& { &&&&&&& & ShowMessage("网络传输错误"); &&&&&&&& } &&} &&catch(Exception e) &&{ &&&Log.i("ex", e.getMessage()); &&}
阅读(...) 评论()webservice的 发布一般都是使用WSDL(web service descriptive language)文件的样式来发布的,在WSDL文件里面,包含这个webservice暴露在外面可供使用的接口。今天搜索到了非常好的 webservice provider列表
http://www.webservicex.net/WCF/default.aspx
这上面列出了70多个包括很多方面的free webservice provider,utilities-&global weather就可以获取全球的天气预报。
下面我们来看Java如何通过WSDL文件来调用这些web service:
注意,以下的代码并没有经过真正的测试,只是说明这些情况,不同版本的Axis相差很大,大家最好以apache网站上的例子为准,这里仅仅用于说明其基本用法。
1,直接AXIS调用远程的web service
我觉得这种方法比较适合那些高手,他们能直接看懂XML格式的WSDL文件,我自己是看不懂的,尤其我不是专门搞这行的,即使一段时间看懂,后来也就忘记了。直接调用模式如下:
import java.util.D
import java.text.DateF
import org.apache.axis.client.C
import org.apache.axis.client.S
import javax.xml.namespace.QN
import java.lang.I
import javax.xml.rpc.ParameterM
public class caClient {
public static void main(String[] args) {
String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";
//直接引用远程的wsdl文件
//以下都是套路 Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
call.setOperationName("addUser");//WSDL里面描述的接口名称
call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE,
javax.xml.rpc.ParameterMode.IN);//接口的参数
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型
String temp = "测试人员";
String result = (String)call.invoke(new Object[]{temp});
//给方法传递参数,并且调用方法
System.out.println("result is "+result);
catch (Exception e) {
System.err.println(e.toString());
2,直接SOAP调用远程的webservice
这种模式我从来没有见过,也没有试过,但是网络上有人贴出来,我也转过来
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;
import java.io.*;
import java.net.*;
import java.util.V
public class caService{
public static String getService(String user) {
url=new URL("http://192.168.0.100:8080/ca3/services/caSynrochnized");
} catch (MalformedURLException mue) {
return mue.getMessage();
// This is the main SOAP object
Call soapCall = new Call();
// Use SOAP encoding
soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
// This is the remote object we're asking for the price
soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");
// This is the name of the method on the above object
soapCall.setMethodName("getUser");
// We need to send the ISBN number as an input parameter to the method
Vector soapParams = new Vector();
// name, type, value, encoding style
Parameter isbnParam = new Parameter("userName", String.class, user, null);
soapParams.addElement(isbnParam);
soapCall.setParams(soapParams);
// Invoke the remote method on the object
Response soapResponse = soapCall.invoke(url,"");
// Check to see if there is an error, return "N/A"
if (soapResponse.generatedFault()) {
Fault fault = soapResponse.getFault();
String f = fault.getFaultString();
// read result
Parameter soapResult = soapResponse.getReturnValue ();
// get a string from the result
return soapResult.getValue().toString();
} catch (SOAPException se) {
return se.getMessage();
3,使用wsdl2java把WSDL文件转成本地类,然后像本地类一样使用,即可。
这是像我这种懒人最喜欢的方式,仍然以前面的global weather report为例。
首先 java org.apache.axis.wsdl.WSDL2Java http://www.webservicex.net/globalweather.asmx.WSDL
原本的网址是http://www.webservicex.net/globalweather.asmx?WSDL,中间个各问号,但是Linux下面它不能解析,所以去掉问号,改为点号。
那么就会出现4个文件:
GlobalWeather.java GlobalWeatherLocator.java GlobalWeatherSoap.java GlobalWeatherSoapStub.java其中GlobalWeatherSoap.java是我们最为关心的接口文件,如果你对RMI等SOAP实现的具体细节不感兴趣,那么你只需要看接口文件即可,在使用的时候,引入这个接口即可,就好像使用本地类一样。
浏览 63337
浏览: 388863 次
来自: 珠海
没看到word文件啊
如何在java Web项目中开发WebService接口,地址 ...
元素不对应啊
非常好,再次感谢
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'所有回答(3)
原因:浏览器的ajax调用跨域接口,不能用Post调用,只能通过GET调用而且只能用javascript的请求方式.
所以就jsonp
推荐一篇博文:
园豆:36664
园豆:36664
清除回答草稿
&&&您需要以后才能回答,未注册用户请先。没有更多推荐了,
不良信息举报
举报内容:
c#调用JAVA提供的WebService处理日期格式
举报原因:
原文地址:
原因补充:
最多只允许输入30个字
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!璋冪敤webservice姣忔?浼犺繘鐨勫弬鏁伴兘涓簄ull,璇锋暀鏄?粈涔堝師鍥狅紵銆恓ava鍚с}

我要回帖

更多关于 java调用webservice 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信