Android开发WebService基于ksoap2的模式怎样实现超时Toast提示
Android开发WebService基于ksoap2的方式怎样实现超时Toast提示?
怎样在网络进行超市连接的时候给出提示啊???我的代码如下:
------解决思路----------------------
我也遇到这个问题,自己想办法解决了。
http://blog.****.net/wkw1125/article/details/48371553
怎样在网络进行超市连接的时候给出提示啊???我的代码如下:
package com.example.qr_codescan;
public class DengluActivity extends Activity implements View.OnClickListener{
static final String nameSpace = "http://tempuri.org/";
// 调用的方法名称
static final String methodName = "AccessRight";
// EndPoint
//static final String endPoint = "http://59.173.237.218/webservice/WebService.asmx";
static final String endPoint = "http://203.171.224.178/hhlwebservice/WebService.asmx";
// SOAP Action
static final String soapAction = "http://tempuri.org/AccessRight";
private int timeout = 3000;
private long mExitTime;
private TextView banbenhao;
private EditText username,password;
//private Button ButtonOK,ButtonCancle;
private Button button_clear01,button_clear02,login;
private String textAddress = "";
private String TAG = "变量输出结果";
Handler handlerAddress = new Handler() {
public void handleMessage(Message msg) {
//System.out.println("xx0xxIP地址为:"+ strIP);
//System.out.println("验证码为:"+ strVify);
System.out.println("××0××更新号码归属地数据,归属地为:"+ textAddress);
if(textAddress.toString().equals("y"))
{
//Toast.makeText(DengluActivity.this, textAddress, Toast.LENGTH_SHORT).show();
username.setText("");
password.setText("");
Intent intent = new Intent();
intent.setClass(DengluActivity.this, MainActivity.class);
startActivity(intent);
}
else if(textAddress.toString().equals("e"))
{
Toast.makeText(DengluActivity.this, "服务器错误", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(DengluActivity.this, "该用户不存在", Toast.LENGTH_SHORT).show();
}
//}
//bTextView.setText("验证码:" + yanzhengma);
//Toast.makeText(DengluActivity.this, tishi.getText(), Toast.LENGTH_LONG)
// .show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.login);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.denglu_title);
username = (EditText) findViewById (R.id.username);
button_clear01 = (Button) findViewById (R.id.button_clear01);
password = (EditText) findViewById (R.id.password);
button_clear02 = (Button) findViewById (R.id.button_clear02);
login = (Button) findViewById (R.id.login);
banbenhao = (TextView) findViewById(R.id.banbenhao);
new Thread(new Runnable(){
public void run(){
String s = getVersion();
System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"+s+"xxxxxxxxxxxxxxxxxxxxxxx");
banbenhao.setText(s);
System.out.println("cccccccccccccccccc"+s+"cccccccccccccccccccccccc");
}
}).start();
button_clear01.setOnClickListener(this);
button_clear02.setOnClickListener(this);
login.setOnClickListener(this);
checkNetworkState();
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.login:
if(NullNetwork() != false)
{
if(username.getText().toString().length() == 0)
{
Toast.makeText(DengluActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
}
else if(password.getText().toString().length() == 0)
{
Toast.makeText(DengluActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
}
else
{
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
new Thread(new Runnable(){
public void run(){
getTelAddress();
}
}).start();
}
}
break;
}
};
public void getTelAddress()
{
try
{
//ProgressDialogUtils.showProgressDialog(this, "数据加载中...");
System.out.println("××1××进入getTelAddress方法");
SoapObject soapObject
= new SoapObject(nameSpace, methodName);
///这边理论上要做输入验证的,例子图省事没做输入验证验证
soapObject.addProperty("userNameAndPhone", username.getText().toString()+"&"+password.getText().toString());
//soapObject.addProperty("password",password.getText().toString());
System.out.println("数据发送成功"+ username.getText().toString());
//soapObject.addProperty("securityCode", yanzhengma);
//soapObject.addProperty("userId", "");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER12);
envelope.bodyOut = soapObject;
envelope.dotNet = true;
envelope.setOutputSoapObject(soapObject);
//HttpTransportSE httpTransportSE = new HttpTransportSE(endPoint);
int timeout = 10000;
MyAndroidHttpTransport httpTransportSE = new MyAndroidHttpTransport(endPoint,timeout);
System.out.println("××2××基本服务设置完毕,下面开始调用服务");
try
{
httpTransportSE.debug = true;
httpTransportSE.call(soapAction, envelope);
System.out.println("××3××调用webservice服务成功");
}
catch (Exception e)
{
e.printStackTrace();
Toast.makeText(this, "服务器错误", Toast.LENGTH_SHORT).show();
System.out.println("××4××调用webservice服务失败");
}
SoapObject object = (SoapObject) envelope.bodyIn;
System.out.println("××5××获得服务数据成功");
textAddress = object.getProperty(0).toString();
System.out.println("××6××解析服务数据成功,数据为:"+textAddress);
System.out.println("××7××向主线程发送消息,显示号码归属地");
handlerAddress.sendEmptyMessage(0);
System.out.println("××8××向主线程发送消息成功,getTelAddress函数执行完毕");
}
catch(Exception e)
{
Toast.makeText(DengluActivity.this, "服务器错误".toString(), Toast.LENGTH_SHORT);
}
}
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode == KeyEvent.KEYCODE_BACK)
{
if((System.currentTimeMillis()-mExitTime)>2000)
{
Toast.makeText(this,"再按一次退出程序", Toast.LENGTH_SHORT).show();
mExitTime = System.currentTimeMillis();
}
else
{
finish();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
private boolean checkNetworkState() {
boolean flag = false;
//得到网络连接信息
ConnectivityManager manager = (ConnectivityManager)getSystemService(
Context.CONNECTIVITY_SERVICE);
//manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
System.out.println("xxxxxxxxxxxxxxxxxxxxxxx"+manager+"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
//去进行判断网络是否连接
if (manager.getActiveNetworkInfo() != null) {
System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxx"+manager+"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
flag = manager.getActiveNetworkInfo().isAvailable();
}
if (!flag) {
setNetwork();
} else {
isNetworkAvailable();
}
return flag;
}
/**
* 网络未连接时,调用设置方法
*/
private void setNetwork(){
Toast.makeText(this, "wifi已关闭", Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("网络提示信息");
builder.setMessage("网络不可用,如果继续,请先设置网络!");
builder.setPositiveButton("设置", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = null;
/**
* 判断手机系统的版本!如果API大于10 就是3.0+
* 因为3.0以上的版本的设置和3.0以下的设置不一样,调用的方法不同
*/
if (android.os.Build.VERSION.SDK_INT > 10) {
intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
} else {
intent = new Intent();
ComponentName component = new ComponentName(
"com.android.settings",
"com.android.settings.WirelessSettings");
intent.setComponent(component);
intent.setAction("android.intent.action.VIEW");
}
startActivity(intent);
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create();
builder.show();
}
------解决思路----------------------
我也遇到这个问题,自己想办法解决了。
http://blog.****.net/wkw1125/article/details/48371553