您的位置:首页 > 移动开发 > Android开发

Android调用Webservice实现…

2014-12-21 16:46 399 查看
原文地址:Android调用Webservice实现登录功能作者: 用户23203476331.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
4000
_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----------------------------------------




2------------------------------------------------------


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: