`

【android】访问web service(大概功能版)

 
阅读更多
增加功能:
1,用户点击次数过快。提示。
2,增加progressBar在线程连接web service的时候出现。
3,增加ksoap连接超时。默认链接超时时间过长。体验不好。
4,网络超时连接提示。

layout xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- 建立一个progressbar当进程在进行处理的时候会出现 -->
    <ProgressBar 
        android:id="@+id/processBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleSmallTitle" />
    
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/userName"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="UserName" />

    <EditText
        android:id="@+id/userName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:ems="10" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/userName"
        android:layout_marginTop="28dp"
        android:text="PassWord" />

    <EditText
        android:id="@+id/passWord"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignParentRight="true"
        android:ems="10" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="Button" />

    <TextView
        android:id="@+id/retrunValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/passWord"
        android:layout_marginTop="32dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>


package com.duduli.li;

import java.io.IOException;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class MainActivity extends Activity {
	private TextView tv,tv2;
	private Button b;
	private EditText name,password;
	private MyHandler myHandler;
	private HttpTransportSE ht;
	
	private boolean processFlag = true;
//	private Handler handler;
	private SoapSerializationEnvelope envelope;
	private static final String SERVICE_NS = "http://li.duduli.com/";
	private static final String SERVICE_URL="http://10.0.2.2:8080/MyContact/services/LoginImplPort";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//      窗口进度栏可见
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_main);
        setProgressBarIndeterminateVisibility(false);
        
        b = (Button) this.findViewById(R.id.button1);
        tv = (TextView) this.findViewById(R.id.textView1);
        tv2 = (TextView) this.findViewById(R.id.retrunValue);
        name = (EditText) this.findViewById(R.id.userName);
        password = (EditText) this.findViewById(R.id.passWord);
        myHandler = new MyHandler();
        b.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setProgressBarIndeterminateVisibility(true);
				if (processFlag) {  
					processFlag = false;;//  
					new Thread(new MyThread()).start();
	                new TimeThread().start();  
	             }else{
	            	 setProgressBarIndeterminateVisibility(false);
	            	 Toast.makeText(getApplicationContext(), "你点击太快了", Toast.LENGTH_SHORT).show();
	             }
			}
		});
    }

    class MyHandler extends Handler{

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
//			super.handleMessage(msg);
			Bundle b = msg.getData();
//			tv2.setText(b.getString("value")); 
			System.out.println(b.getString("value"));
			
			String str = b.getString("value");
			String flag = str.substring(0, str.indexOf("#"));
			String json = str.substring(str.indexOf("#")+1,str.length());
			System.out.println(flag);
			System.out.println();
			
			
			if("1".equals(flag)){
				Gson g2 = new Gson();
				List<Person> p = g2.fromJson(json,  new TypeToken<List<Person>>(){}.getType());
				System.out.println(p.size());
				for(int i=0;i<p.size();i++){
					System.out.println(p.get(i).getName());
				}
				setProgressBarIndeterminateVisibility(false);
				tv2.setText(p.get(0).getName());
			}else{
				setProgressBarIndeterminateVisibility(false);
				tv2.setText(json);
			}
			
			/* */
//			tv2.setText(b.getString("value"));
		}
    	
    }
    
    class MyThread implements Runnable{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			Message msg = new Message();
			Bundle b = new Bundle();
//			String values = "";
			String values = "0#网络连接失败";
//			超时时间5秒
			ht = new HttpTransportSE(SERVICE_URL,5000);
	        ht.debug = true;
	        //使用soap1.1协议创建Envelop对象
	        envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	        //实例化SoapObject对象
	        SoapObject request = new SoapObject(SERVICE_NS, "reJson");
	        
//	        request.addProperty("arg0", name.getText().toString());
//	        request.addProperty("arg1", password.getText().toString());
	        
	        envelope.bodyOut = request;
			//你要执行的方法
				try {
					ht.call(null, envelope);
					if(envelope.getResponse()!=null){
						SoapObject result = (SoapObject) envelope.bodyIn;
						values = result.getProperty(0).toString();
		               
					}else{
						values = "nothing";
					}
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (XmlPullParserException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				b.putString("value", values);
				msg.setData(b);
				myHandler.sendMessage(msg);
		}
    	
    }
    
    
    /** 
     * 计时线程(防止在一定时间段内重复点击按钮) 
     */  
	private class TimeThread extends Thread {
		public void run() {
			try {
				sleep(1000);
				processFlag = true;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}


在Androidmanifest中记得加入网络读的权限。
<uses-permission android:name="android.permission.INTERNET" />
分享到:
评论

相关推荐

    Android客户端访问Web Service的实现.pdf

    在探讨Android客户端如何访问Web Service时,首先需要了解Android平台的基础知识以及Web Service的相关技术细节。以下知识点是根据文档提供的标题、描述、标签以及部分内容,按照要求详细说明Android客户端访问Web ...

    Android访问 web service asmx

    Web Service ASMX 是一种基于.NET框架的Web服务技术,它允许开发者创建和部署Web服务,供其他应用程序(包括Android应用)调用。本教程将深入讲解如何在Android应用中访问ASMX类型的Web服务。 首先,我们要介绍关键...

    Android 请求 WebAPI的案例

    本案例"Android请求WebAPI"将详细讲解如何在Android应用中实现这一功能。WebAPI通常指的是基于HTTP协议的RESTful API,允许客户端(如Android应用)通过HTTP方法(GET、POST、PUT、DELETE等)获取或操作服务器资源。...

    Android 通过 Ksoap2 访问 Web Service Demo

    通过以上步骤,你就可以利用Ksoap2在Android应用中访问Web Service,查询手机号码归属地。记住,实践是最好的老师,动手尝试编写代码并调试,遇到问题时查阅官方文档或社区资源,会进一步加深对Ksoap2的理解。

    android 资料整理 web service

    在Android开发中,Web Service是一种常见的技术,用于让Android客户端应用程序与远程服务器进行通信,获取或发送数据。这里我们主要探讨Android与Web Service的交互,以及相关知识点。 首先,理解Web Service的概念...

    从android中调用web service的源码.zip

    在Android开发中,调用Web Service是常见的需求,主要用于实现移动设备与服务器之间的数据交互。Web Service通常基于SOAP(Simple Object Access Protocol)或REST(Representational State Transfer)协议,为不同...

    基于Android和Web Service的掌上校园系统的设计与实现.pdf

    "基于Android和Web Service的掌上校园系统的设计与实现" 本文主要介绍了基于Android和Web Service的掌上校园系统的设计与实现。随着移动互联网的高速发展和智能手机的普及,广大师生迫切希望能够通过手机平台来随时...

    Android客户端访问服务器端的Web Service所需的KSoap包

    Web Service是一种常见的通信方式,而KSoap2则是Android平台下访问SOAP Web Service的首选库。本篇将详细介绍如何利用KSoap2库实现在Android客户端访问服务器端的Web Service。 首先,我们需要了解什么是SOAP...

    Android通过Webservices访问网络资源

    综上所述,Android通过Webservices访问网络资源涉及网络编程基础、权限管理、数据解析、UI绑定等多个环节。了解并熟练掌握这些知识点,对于开发能高效访问网络的Android应用至关重要。在实际开发中,结合具体项目...

    android访问webservices详细例子

    以上就是Android访问Web Services的一些关键步骤和注意事项。在实际项目中,还可能涉及数据缓存、网络状态监测、数据同步策略等复杂问题。学习和熟练掌握这些技术,将有助于构建功能完善的Android应用。

    安卓Android源码——从android中调用web service的源码.zip

    在安卓(Android)平台上开发应用时,调用Web Service是一项常见的任务,这通常涉及到与远程服务器进行数据交互,实现功能如登录验证、数据同步等。本压缩包中的源码提供了从Android应用程序中调用Web Service的示例...

    访问web service代码

    访问web service代码

    android web service 例子源代码

    在Android开发中,Web服务是将移动应用与服务器端数据进行交互的重要手段。Web服务允许Android设备通过HTTP协议发送请求并接收响应,实现数据的获取、更新或删除。本资源包含了一个具体的Android Web服务示例的源...

    Android学习平台中Web Service架构的实现与研究.pdf

    在Android学习平台的开发中,Web Service架构起着至关重要的作用,它使得移动设备能够与服务器进行高效的数据交换,从而实现远程教育平台的功能。Web Service是一种基于XML的通信技术,允许不同系统间的数据交互,不...

    【android】web service访问

    NULL 博文链接:https://duduli.iteye.com/blog/1705770

    delphi XE5 ANDROID平台 调用 webservice并访问操作MSSQL数据库

    Delphi XE5 Android 平台调用 Webservice 并访问操作 MSSQL 数据库 Delphi XE5 是一款功能强大且灵活的开发环境,为开发者提供了跨平台的开发体验。在 Android 平台上,Delphi XE5 提供了强大的支持,允许开发者...

    android 访问webService Demo

    二、Android访问Web Service的基本步骤 1. 添加网络权限:在AndroidManifest.xml文件中,添加`&lt;uses-permission android:name="android.permission.INTERNET" /&gt;`以允许应用访问网络。 2. 选择库:Android提供了...

    从android中调用web service的源码

    在Android开发中,调用Web Service是常见的数据交互方式,特别是对于需要远程获取或发送数据的应用来说至关重要。Web Service通常采用SOAP(Simple Object Access Protocol)或REST(Representational State ...

    安卓开发-从android中调用web service的源码.zip

    在Android开发中,调用Web Service是常见的数据交互方式,特别是在需要与远程服务器进行数据交换时。本资源“安卓开发-从android中调用web service的源码.zip”提供了具体的实现示例,帮助开发者理解这一过程。以下...

Global site tag (gtag.js) - Google Analytics