Android利用SOAP进行网络编程,面试必问知识点

android:id=“@+id/textView1”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_centerHorizontal=“true”

android:layout_centerVertical=“true”

android:padding=“@dimen/padding_medium”

tools:context=“.AndroidSoapActivity” />

输入控件,用户输入城市名称:

<EditText

android:id=“@+id/cityName”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignParentLeft=“true”

android:layout_alignParentTop=“true”

android:text=“@string/cityName” />

按钮,用户提交城市名称时候单击该按钮:

<Button

android:id=“@+id/ok”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignParentTop=“true”

android:layout_toRightOf=“@+id/textView1”

android:text=“@string/search” />

(4)完成应用内部对查询处理的主要代码:

import java.io.UnsupportedEncodingException;

import org.ksoap2.SoapEnvelope;

import org.ksoap2.serialization.SoapObject;

import org.ksoap2.serialization.SoapSerializationEnvelope;

import org.ksoap2.transport.HttpTransportSE;

import android.os.AsyncTask;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

SOAP方式查询天气情况包括:指定命名空间、给出接口地址、设置方法名、设置查询接口参数

public class AndroidSoapActivity extends Activity {

private static final String NAMESPACE = “http:// WebXml.com.cn/”;

private static String URL = “http://www.webxml.com.cn/webservices/weatherwebservice.asmx”; private static final String METHOD_NAME = “getWeatherbyCityName”;

private static String SOAP_ACTION = “http:// WebXml.com.cn/getWeatherbyC-ityName”;

private String weatherToday;

private Button okButton;

private SoapObject detail;

private EditText cityNameText;

private TextView cityMsgView;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_android_soap);

cityNameText =(EditText)findViewById(R.id.cityName);

cityMsgView = (TextView)findViewById(R.id.textView1);

okButton = (Button) findViewById(R.id.ok);

okButton.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v) {

new showWeatherAsyncTask().execute();

}

});

}

使用AsyncTask异步方式获取并显示天气信息

private class showWeatherAsyncTask extends AsyncTask<String, Integer, String> {

@Override

protected String doInBackground(String… Urls) {

showWeather();

return null;

}

protected void onPostExecute(String result) {

}

};

private void showWeather() {

String city = cityNameText.getText().toString().trim();

if(!city.isEmpty()){ getWeather(city);

}

}

public void getWeather(String cityName) {

try {

SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);

rpc.addProperty(“theCityName”, cityName);

HttpTransportSE ht = new HttpTransportSE(URL);

ht.debug = true;

SoapSerializationEnvelopeenvelope=

newSoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.bodyOut = rpc;

envelope.dotNet = true;

envelope.setOutputSoapObject(rpc);

ht.call(SOAP_ACTION, envelope);

SoapObject result = (SoapObject) envelope.bodyIn;

detail = (SoapObject)result.getProperty(“getWeatherbyCityNameResult”); System.out.println(“detail” + detail);

parseWeather(detail);

return;

}

catch (Exception e) {

e.printStackTrace();

}

}

解析SoapObject对象

private void parseWeather(SoapObject detail)throws UnsupportedEncodingException {

String date = detail.getProperty(6).toString();

weatherToday = “今天:” + date.split(" ")[0];

weatherToday = weatherToday + " 天气:" + date.split(" ")[1];

weatherToday = weatherToday + " 气温:" + detail.getProperty(5).toString() ;

weatherToday = weatherToday + " 风力:" + detail.getProperty(7).toString()+ " "; System.out.println("weatherToday is " + weatherToday);

cityMsgView.setText(weatherToday);

}

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

最后

7)]
[外链图片转存中…(img-WGdA11Rv-1710776435787)]
[外链图片转存中…(img-v7aekv0G-1710776435788)]
[外链图片转存中…(img-hIkyGP8R-1710776435788)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
[外链图片转存中…(img-tOewzjJ9-1710776435789)]

最后

想要了解更多关于大厂面试的同学可以**点击这里免费获取《面试文档》**除此之外,我也分享一些免费的优质资源,包括:Android学习PDF+架构视频+源码笔记高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料 这几块的内容。分享给大家,非常适合近期有面试和想在技术道路上继续精进的朋友。快来获取学习资料吧~

  • 20
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值