android的互联网开发 下
http1.xml <TableRow> <TextView android:text="用户密码:" android:id="@+id/TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" ></TextView> <EditText android:text="" android:id="@+id/pwdEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:password="true"></EditText> </TableRow> <TableRowandroid:gravity="right"> <Button android:text="取消" android:id="@+id/cancelButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="登陆" android:id="@+id/loginButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </TableRow> </TableLayout> /LinearLayout> 四、Web Service编程 TestWebServiceActivity.java packagecom.amaker.ch13.webservice; importjava.io.IOException; importorg.ksoap2.SoapEnvelope; importorg.ksoap2.serialization.MarshalBase64; importorg.ksoap2.serialization.PropertyInfo; importorg.ksoap2.serialization.SoapObject; importorg.ksoap2.serialization.SoapSerializationEnvelope; importorg.ksoap2.transport.AndroidHttpTransport; importorg.xmlpull.v1.XmlPullParserException; importandroid.app.Activity; importandroid.os.Bundle; publicclassTestWebServiceActivityextendsActivity{ @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); StringserviceNamespace="http://tempuri.org/"; StringserviceURL="http://www.ayandy.com/Service.asmx"; StringmethodName="getWeatherbyCityName"; SoapObjectrequest=newSoapObject(serviceNamespace,methodName); PropertyInfoinfo=newPropertyInfo(); info.setName("theCityName"); info.setValue("北京"); PropertyInfoinfo2=newPropertyInfo(); info2.setName("theDayFlag"); info2.setValue("1"); request.addProperty(info); request.addProperty(info2); SoapSerializationEnvelopeenvelope=newSoapSerializationEnvelope(SoapEnvelope.VER11); envelope.bodyOut=request; (newMarshalBase64()).register(envelope); AndroidHttpTransportht=newAndroidHttpTransport(serviceURL); ht.debug=true; try{ ht.call("http://tempuri.org/getWeatherbyCityName",envelope); if(envelope.getResponse()!=null){ System.out.println(envelope.getResult()); } }catch(IOExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); }catch(XmlPullParserExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } } WeatherActivity.java packagecom.amaker.ch13.webservice; importjava.util.List; importandroid.app.Activity; importandroid.os.Bundle; importandroid.view.View; importandroid.widget.AdapterView; importandroid.widget.ArrayAdapter; importandroid.widget.Spinner; importandroid.widget.TextView; importandroid.widget.AdapterView.OnItemSelectedListener; importcom.amaker.ch13.R; /** * *显示天气预报 */ publicclassWeatherActivityextendsActivity{ //声明视图组件 privateTextViewdisplayTextView; privateSpinnerspinner; @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.weather); //实例化视图组件 displayTextView=(TextView)findViewById(R.id.displayTextView03); spinner=(Spinner)findViewById(R.id.citySpinner01); List<String>citys=WebServiceUtil.getCityList(); ArrayAdaptera=newArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item,citys); spinner.setAdapter(a); spinner.setOnItemSelectedListener(newOnItemSelectedListener(){ @Override publicvoidonItemSelected(AdapterView<?>arg0,Viewarg1, intarg2,longarg3){ Stringmsg=WebServiceUtil.getWeatherMsgByCity(spinner.getSelectedItem().toString()); displayTextView.setText(msg); } @Override publicvoidonNothingSelected(AdapterView<?>arg0){ } }); } } WebServiceUtil.java packagecom.amaker.ch13.webservice; importjava.io.IOException; importjava.io.InputStream; importjava.util.ArrayList; importjava.util.List; importjavax.xml.parsers.DocumentBuilder; importjavax.xml.parsers.DocumentBuilderFactory; importorg.apache.http.HttpResponse; importorg.apache.http.NameValuePair; importorg.apache.http.client.entity.UrlEncodedFormEntity; importorg.apache.http.client.methods.HttpPost; importorg.apache.http.impl.client.DefaultHttpClient; importorg.apache.http.message.BasicNameValuePair; importorg.apache.http.protocol.HTTP; importorg.apache.http.util.EntityUtils; importorg.ksoap2.SoapEnvelope; importorg.ksoap2.serialization.MarshalBase64; importorg.ksoap2.serialization.SoapObject; importorg.ksoap2.serialization.SoapSerializationEnvelope; importorg.ksoap2.transport.AndroidHttpTransport; importorg.w3c.dom.Document; importorg.w3c.dom.Element; importorg.w3c.dom.Node; importorg.w3c.dom.NodeList; importorg.xmlpull.v1.XmlPullParserException; /** * *天气预报工具类 */ publicclassWebServiceUtil{ /* *通过传递城市名称获得天气信息 */ publicstaticStringgetWeatherMsgByCity(StringcityName){ Stringurl="http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather"; HttpPostrequest=newHttpPost(url); List<NameValuePair>params=newArrayList<NameValuePair>(); params.add(newBasicNameValuePair("theCityCode",cityName)); params.add(newBasicNameValuePair("theUserID","")); Stringresult=null; try{ UrlEncodedFormEntityentity=newUrlEncodedFormEntity(params, HTTP.UTF_8); request.setEntity(entity); HttpResponseresponse=newDefaultHttpClient().execute(request); if(response.getStatusLine().getStatusCode()==200){ result=EntityUtils.toString(response.getEntity()); returnparse2(result); } }catch(Exceptione){ e.printStackTrace(); } returnnull; } /* *使用ksoap,获得城市列表 */ publicstaticList<String>getCityList(){ //命名空间 StringserviceNamespace="http://WebXml.com.cn/"; //请求URL StringserviceURL="http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx"; //调用的方法 StringmethodName="getRegionProvince"; //实例化SoapObject对象 SoapObjectrequest=newSoapObject(serviceNamespace,methodName); //获得序列化的Envelope SoapSerializationEnvelopeenvelope=newSoapSerializationEnvelope( SoapEnvelope.VER11); envelope.bodyOut=request; (newMarshalBase64()).register(envelope); //Android传输对象 AndroidHttpTransportht=newAndroidHttpTransport(serviceURL); ht.debug=true; try{ //调用 ht.call("http://WebXml.com.cn/getRegionProvince",envelope); if(envelope.getResponse()!=null){ returnparse(envelope.bodyIn.toString()); } }catch(IOExceptione){ e.printStackTrace(); }catch(XmlPullParserExceptione){ e.printStackTrace(); } returnnull; } /* *对天气信息XML文件进行解析 */ privatestaticStringparse2(Stringstr){ Stringtemp; String[]temps; Listlist=newArrayList(); StringBuildersb=newStringBuilder(""); if(str!=null&&str.length()>0){ temp=str.substring(str.indexOf("<string>")); temptemps=temp.split("</string>"); for(inti=0;i<temps.length;i++){ sb.append(temps[i].substring(12)); sb.append("\n"); } } returnsb.toString(); } /* *对得到的城市XML信息进行解析 */ privatestaticList<String>parse(Stringstr){ Stringtemp; List<String>list=newArrayList<String>(); if(str!=null&&str.length()>0){ intstart=str.indexOf("string"); intend=str.lastIndexOf(";"); temp=str.substring(start,end-3); String[]test=temp.split(";"); for(inti=0;i<test.length;i++){ if(i==0){ temp=test[i].substring(7); }else{ temp=test[i].substring(8); } intindex=temp.indexOf(","); list.add(temp.substring(0,index)); } } returnlist; } } weather.xml <?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:text="天气预报" android:id="@+id/titleTextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:text="请选择城市:" android:id="@+id/cityTextView02" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <Spinner android:id="@+id/citySpinner01" android:layout_width="fill_parent" android:layout_height="wrap_content"></Spinner> </LinearLayout> <ScrollView android:id="@+id/ScrollView01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="@+id/displayTextView03" android:id="@+id/displayTextView03" android:layout_width="fill_parent" android:layout_height="fill_parent"></TextView> </ScrollView> </LinearLayout> 五、WebView编程 TestWebViewActivity.java packagecom.amaker.ch13.webview; importandroid.app.Activity; importandroid.os.Bundle; importandroid.webkit.WebView; importcom.amaker.ch13.R; /** *通过WebView浏览网络 */ publicclassTestWebViewActivityextendsActivity{ privateWebViewwebView; @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.test_webview); webView=(WebView)findViewById(R.id.mywebview); /*Stringurl="http://www.google.com"; webView.loadUrl(url);*/ Stringhtml=""; html+="<html>"; html+="<body>"; html+="<ahref=http://www.google.com>GoogleHome</a>"; html+="</body>"; html+="</html>"; webView.loadData(html,"text/html","utf-8"); } } test_webview.xml <?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <WebView android:id="@+id/mywebview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> 本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1079312