android-基于Webservice实现天气信息获取及源码下载(三)
本项目说明如下:
1、分别使用三种方式获取服务信息。soap、http get、http post三种方式实现信息的获取。
2、基于android调用webservice服务详细步骤实现
博文链接上一篇地址:
android-基于Webservice实现天气信息获取及源码下载(一)
android-基于Webservice实现天气信息获取及源码下载(二)
接上一篇博文讲述,省份列表和城市列表都是用了Adapter类,其实比较简单,代码如下:
public class ProviceOrCityAdapter extends BaseAdapter {
private ArrayList<String> arrayList;
private Context context;
public ProviceOrCityAdapter(Context context,ArrayList<String> arrayList){
this.context = context;
this.arrayList = arrayList;
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return arrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if (convertView == null) {
convertView = View.inflate(context, R.layout.main_list_item, null);
holder = new Holder();
holder.text = (TextView) convertView.findViewById(R.id.provice_tx);
convertView.setTag(holder);
}else {
holder = (Holder) convertView.getTag();
}
holder.text.setText(arrayList.get(position));
return convertView;
}
class Holder{
TextView text;
}
}
Adapter类非常简单,就是保存一个文本,其实完全不用使用BaseAdapter,大家可以尝试使用ListAdapter类都可以。
下面给出城市县列表代码:
public class CityListActivity extends Activity {
private ListView cityList;
private TextView backTx;
private ArrayList<String> cityArrayList = new ArrayList<>();
private ProviceOrCityAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.city_activity_listv);
init();
setClick();
//HTTP GET方式获取城市列表
// HttpUtils.GetCityGet(getIntent().getStringExtra("provice"));
//HTTP POST方式获取城市列表
// HttpUtils.GetCityPost(getIntent().getStringExtra("provice"));
final WebServiceUtil webServiceUtil = new WebServiceUtil();
webServiceUtil.setCityCallBack(new CallBack() {
@Override
public void getData(ArrayList<String> arrayList) {
CityListActivity.this.cityArrayList = arrayList;
adapter = new ProviceOrCityAdapter(CityListActivity.this, arrayList);
cityList.setAdapter(adapter);
}
}, getIntent().getStringExtra("provice"));
}
private void setClick() {
backTx.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
cityList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(CityListActivity.this, cityArrayList.get(position), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(CityListActivity.this, WeatherActivity.class);
intent.putExtra("theRedionCode", getIntent().getStringExtra("provice"));
startActivity(intent);
}
});
}
private void init(){
cityList = (ListView) findViewById(R.id.city_list);
backTx = (TextView) findViewById(R.id.back);
adapter = new ProviceOrCityAdapter(CityListActivity.this, cityArrayList);
cityList.setAdapter(adapter);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
不难,不多做解释。
下面是WeatherActivity类天气信息获取
代码如下:
public class WeatherActivity extends Activity {
private TextView back;
private TextView weatherTx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.weather_activity_view);
init();
setclick();
//获取天气信息GET方式
// HttpUtils.GetWeatherGet(getIntent().getStringExtra("theRedionCode"));
//获取天气信息POST方式
// HttpUtils.GetWeatherPost(getIntent().getStringExtra("theRedionCode"));
final WebServiceUtil webServiceUtil = new WebServiceUtil();
webServiceUtil.setWeatherCallBack(new WeatherCallBack() {
@Override
public void getData(String weather) {
weatherTx.setText(weather);
}
}, getIntent().getStringExtra("theRedionCode"));
}
private void setclick() {
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
private void init() {
back = (TextView) findViewById(R.id.back);
weatherTx = (TextView) findViewById(R.id.text);
}
}
我这里偷了个懒,只是把天气信息获取到用文本显示出来的。
本项目实现的是三种方式获取数据,没有讲述如何三种方式获取,下面给出使用HTTP GET、HTTP POST两种方式的获取代码如下:
public class HttpUtils {
private static String TAG = HttpUtils.class.getSimpleName();
/**
* 获取省份
* post方式
*/
public static void GetProvicePost(){
Log.i(TAG, "HttpUtils==GetProvicePost");
new Thread(){
@Override
public void run() {
super.run();
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(Constants.URI+"/getRegionProvince");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("Content-Type", "application/x-www-form-urlencoded"));
params.add(new BasicNameValuePair("Content-Length", params.toString().length()+""));
try {
httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() ==200) {
String msg = EntityUtils.toString(httpResponse.getEntity());
Log.i(TAG, "MSG="+msg);
}else {
Log.i(TAG, "获取失败");
}
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, "获取异常1");
}
}
}.start();
}
/**
* 获取省份
* GET方式
*/
public static void GetProviceGet(){
Log.i(TAG, "HttpUtils==GetProviceGet");
new Thread(){
@Override
public void run() {
super.run();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(Constants.URI+"/getRegionProvince?");
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
BufferedReader bReader = new BufferedReader(new InputStreamReader(httpEntity.getContent(),Charset.forName("UTF-8")));
String line = null;
while ((line = bReader.readLine()) != null) {
Log.i(TAG, line);
}
}
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, "获取异常2");
}
}
}.start();
}
/**
* 获取城市
* GET方式
*/
public static void GetCityGet(final String theRegionCode){
Log.i(TAG, "HttpUtils==GetCityGet");
new Thread(){
@Override
public void run() {
super.run();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(Constants.URI+"/getSupportCityString?theRegionCode="+theRegionCode);
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
BufferedReader bReader = new BufferedReader(new InputStreamReader(httpEntity.getContent(),Charset.forName("UTF-8")));
String line = null;
while ((line = bReader.readLine()) != null) {
Log.i(TAG, line);
}
}
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, "获取异常2");
}
}
}.start();
}
/**
* 获取城市
* post方式
*/
public static void GetCityPost(final String theRegionCode){
Log.i(TAG, "HttpUtils==GetCityPost");
new Thread(){
@Override
public void run() {
super.run();
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(Constants.URI+"/getSupportCityString");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("Content-Type", "application/x-www-form-urlencoded"));
params.add(new BasicNameValuePair("Content-Length", params.toString().length()+""));
params.add(new BasicNameValuePair("theRegionCode",theRegionCode));
try {
httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() ==200) {
String msg = EntityUtils.toString(httpResponse.getEntity());
Log.i(TAG, "MSG="+msg);
}else {
Log.i(TAG, "获取失败");
}
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, "获取异常1");
}
}
}.start();
}
/**
* 获取天气信息
* post方式
*/
public static void GetWeatherPost(final String theCityCode){
Log.i(TAG, "HttpUtils==GetWeatherPost");
new Thread(){
@Override
public void run() {
super.run();
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(Constants.URI+"/getWeather");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("Content-Type", "application/x-www-form-urlencoded"));
params.add(new BasicNameValuePair("Content-Length", params.toString().length()+""));
params.add(new BasicNameValuePair("theCityCode",theCityCode));
params.add(new BasicNameValuePair("theUserID",Constants.theUserID));
try {
httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() ==200) {
String msg = EntityUtils.toString(httpResponse.getEntity());
Log.i(TAG, "MSG="+msg);
}else {
Log.i(TAG, "获取失败");
}
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, "获取异常1");
}
}
}.start();
}
/**
* 获取天气信息
* GET方式
*/
public static void GetWeatherGet(final String theCityCode){
Log.i(TAG, "HttpUtils==GetWeatherGet");
new Thread(){
@Override
public void run() {
super.run();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(Constants.URI+"/getWeather?theCityCode="+theCityCode+"&theUserID="+Constants.theUserID);
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
BufferedReader bReader = new BufferedReader(new InputStreamReader(httpEntity.getContent(),Charset.forName("UTF-8")));
String line = null;
while ((line = bReader.readLine()) != null) {
Log.i(TAG, line);
}
}
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, "获取异常2");
}
}
}.start();
}
}
该类实现了网络访问天气信息的数据。可能大家会问,该类并没有把数据进行解析,只是简单的把获取到的数据打印出来?其实获取到的数据和使用第一种方式soap方式是一样的。
数据如下:
当你打印出来这样的数据的时候,说明你已经成功了。
不过最后提醒一点就是加权限
博文链接:
android-基于Webservice实现天气信息获取及源码下载(一)
android-基于Webservice实现天气信息获取及源码下载(二)
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。