android 4.0以上版本调研webservice
来源:互联网 发布:golang recover 编辑:程序博客网 时间:2024/06/05 05:23
最近用android 4.0写个小程序,从网上找了很多访问webservice的例子都不成功,后来通过错误查询到,主要是我的版本过高了,通过多次调试终于实现传数了,就此记录下来。
其中遇到两个难题,1、需要建立线程获取webservice数据,不然报错 2、必须用Handler indicate = new Handler(); 从线程中把数据付给控件,要不报错
1、activity_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"
android:paddingTop="5dip"
android:paddingLeft="5dip"
android:paddingRight="5dip"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="手机号码(段):"
/>
<EditText android:id="@+id/phone_sec"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPhonetic"
android:singleLine="true"
android:hint="例如:1398547"
/>
<Button android:id="@+id/query_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="查询"
/>
<TextView android:id="@+id/result_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
/>
</LinearLayout>
2、MainActivity
package com.example.webservice1;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText phoneSecEditText;
private TextView resultView;
private Button queryButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phoneSecEditText = (EditText) findViewById(R.id.phone_sec);
resultView = (TextView) findViewById(R.id.result_text);
queryButton = (Button) findViewById(R.id.query_btn);
queryButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//用Handler 获取线程获取的数据,将数据赋值给控件
Handler indicate = new Handler();
new Thread(downloadRun).start();
}
});
}
public void getRemoteInfo(String phoneSec) {
// 命名空间
String nameSpace = "http://tempuri.org/";
// 调用的方法名称
String methodName = "HelloWorld";
// EndPoint
String endPoint = "http://192.168.0.145/webservice/WebService1.asmx";
// SOAP Action
String soapAction = "http://tempuri.org/HelloWorld";
// 指定WebService的命名空间和调用的方法名
SoapObject rpc = new SoapObject(nameSpace, methodName);
// 设置需调用WebService接口需要传入的两个参数mobileCode、userId
rpc.addProperty("user", phoneSec);
// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.bodyOut = rpc;
// 设置是否调用的是dotNet开发的WebService
envelope.dotNet = true;
// 等价于envelope.bodyOut = rpc;
envelope.setOutputSoapObject(rpc);
HttpTransportSE transport = new HttpTransportSE(endPoint);
try {
// 调用WebService
transport.call(soapAction, envelope);
} catch (Exception e) {
e.printStackTrace();
}
// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
int count1=object.getPropertyCount();
// 获取返回的结果
String result = object.getProperty(0).toString();
Message msg = new Message();
msg.what = 0;
Bundle bundle = new Bundle();
bundle.putString("1", result);
msg.setData(bundle);
handler.sendMessage(msg);
// Toast.makeText(getApplicationContext(),result,Toast.LENGTH_SHORT);
// 将WebService返回的结果显示在TextView中
//resultView.setText(result);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/** * 下载线程 */
Runnable downloadRun = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
//updateListView();
// 手机号码(段)
String phoneSec = phoneSecEditText.getText().toString().trim();
// 简单判断用户输入的手机号码(段)是否合法
if ("".equals(phoneSec) || phoneSec.length() < 7) {
// 给出错误提示
phoneSecEditText.setError("您输入的手机号码(段)有误!");
phoneSecEditText.requestFocus();
// 将显示查询结果的TextView清空
resultView.setText("");
return;
}
// 查询手机号码(段)信息
getRemoteInfo(phoneSec);
}
};
public Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case 0:
{
//取出参数更新控件
resultView.setText(msg.getData().getString("1"));
}
break;
default:
break;
}
super.handleMessage(msg);
}
};
}
3、webservice
- android 4.0以上版本调研webservice
- Android调用webService方法4.0以上可用
- Android Gallery画廊 兼容4.0以上版本
- Android 各版本差异性调研
- android 4.0以上版本不能联网,简单粗暴的解决方法
- MyEclipse8.5及以上版本创建webservice的简单实例
- Android APK 签名(1.5以上版本)
- ANDROID 自动接听 支持2.3以上版本
- android 4.2以上版本永不休眠
- android 4.2以上版本永不休眠
- android 4.2以上版本永不休眠 .
- android 4.2以上版本永不休眠
- android 4.2以上版本永不休眠
- android 4.2以上版本永不休眠
- android 4.2以上版本永不休眠
- MTK Android 5.0以上版本的变化
- MTK Android 5.0以上版本的变化
- android studio配置butterknife 8.0以上版本
- The Swift Programming Language 中文翻译版
- Android HAL开发详解
- CocoaPods安装和使用教程
- 使用SkinMagic美化VC界面
- html 空格
- android 4.0以上版本调研webservice
- Python2.7.6下安装setuptools时报UnicodeDecodeError解决办法
- ubantu相关使用
- c++拷贝构造函数和赋值函数
- 水晶报表CrystalReport2008使用JDBC连接MySQL数据库-详细步骤
- .Net System.Web.Mail发送邮件
- poj-1573
- 专家讲堂系列:虚拟化和云中的大数据-路广(Vmware)
- ios Crash Log 分析汇总