Android调用Webservice实现…

1.2013年1月1日开始接触android开发,7月份就开始做第一份工作,以后会遇到解决掉的问题会及时写出来,以便于需要的人阅读,也当是我一个回顾和复习!


一,android调用C#写的WebService,服务端返回的是一个xml格式的数据,只需要解析xml即可!!下面贴出源码

1.就一个Activity,主要实现的功能就是获取布局文件输入传给服务器的值

public class MainActivity extends Activity {
    private EditText nameEditText;
    private EditText passEditText;
    private TextView textView;
    private Button button;
     
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        nameEditText = (EditText) this.findViewById(R.id.name);
        passEditText = (EditText) this.findViewById(R.id.pass);
        textView = (TextView) this.findViewById(R.id.textView);
        button = (Button) findViewById(R.id.button);
    }
     
    public void query(View v){
    String name = nameEditText.getText().toString();
    String pass = passEditText.getText().toString();
    try {
   
String address = AddressService.getAddress(name,pass);
String result = address.toString();
if (result.equals("true")) {
Toast.makeText(MainActivity.this, "跳转成功", 1).show();
}
textView.setText(address);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), R.string.error, 1).show();
}
    }
}

2.main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
     
    <EditText  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/name"
    android:hint="请填写用户名"
    />
    <EditText  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/pass"
    android:hint="请填写密码"
    />
     
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="query"
        android:text="查询" />
     
    <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView"
    />
</LinearLayout>


3.服务器返回的数据也就是需要解析xml格式的数据(此段代码是webservice soap1.2服务器返回的xml格式的数据)

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <loing xmlns="http://tempuri.org/">
      <name>$name</name>
      <pass>$pass</pass>
    </loing>
  </soap12:Body>
</soap12:Envelope>

4.使用pull方法解析xml数据(访问网络如果放在主线程里面会报NetWork错误,原因使android4.0以后不可以直接在主线程里面访问网络)

public class AddressService {
*/
public static String getAddress(String name,String pass) throws Exception{
String soap = readSoap();
soap = soap.replaceAll("\$name", name);
soap = soap.replaceAll("\$pass", pass);
byte[] entity = soap.getBytes();
String path = "###############";//API地址也就是webservice的访问地址
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
conn.getOutputStream().write(entity);
if(conn.getResponseCode() == 200){
return parseSOAP(conn.getInputStream());
}
return null;
}
private static String parseSOAP(InputStream xml)throws Exception{
XmlPullParser pullParser = Xml.newPullParser();
pullParser.setInput(xml, "UTF-8");
int event = pullParser.getEventType();
while(event != XmlPullParser.END_DOCUMENT){
switch (event) {
case XmlPullParser.START_TAG:
//此处是pull解析xml由于我的项目返回的是一个boolean值,"logigResult"(截图看)
if("loingResult".equals(pullParser.getName())){
return pullParser.nextText();
}
break;
}
event = pullParser.next();
}
return null;
}

private static String readSoap() throws Exception{
InputStream inStream = AddressService.class.getClassLoader().getResourceAsStream("soap12.xml");
byte[] data = StreamTool.read(inStream);
return new String(data);
}
}

5.工具类

public class StreamTool {
public static byte[] read(InputStream inStream) throws Exception{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len = inStream.read(buffer)) != -1){
outputStream.write(buffer, 0, len);
}
inStream.close();
return outputStream.toByteArray();
}

}

截图:
1----------------------------------------
[转载]Android调用Webservice实现登录功能

2------------------------------------------------------
[转载]Android调用Webservice实现登录功能




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值