Android-obtain weather information and download the source code based on Webservice (3)

Source: Internet
Author: User

Android-obtain weather information and download the source code based on Webservice (3)

This project is described as follows:
1. You can obtain service information in three ways. Soap, http get, and http post are used to obtain information.
2. Detailed steps for webservice calling Based on android
 

As described in the previous blog post, the province list and city list both use the Adapter class. The Code is as follows:

public class ProviceOrCityAdapter extends BaseAdapter {    private ArrayList
  
    arrayList;    private Context context;    public ProviceOrCityAdapter(Context context,ArrayList
   
     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;    }}
   
  

The Adapter class is very simple, that is, to save a text. In fact, BaseAdapter is not used at all. You can try ListAdapter class.
The following code lists cities and counties:

Public class CityListActivity extends Activity {private ListView cityList; private TextView backTx; private ArrayList
  
   
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 (); // obtain the city list through http get // HttpUtils. getCityGet (getIntent (). getStringExtra (provice); // obtain the city list in http post mode // HttpUtils. getCityPost (getIntent (). getStringExtra (provice); final WebServiceUtil webServiceUtil = new WebServiceUtil (); webServiceUtil. setCityCallBack (new CallBack () {@ Override public void getData (ArrayList
   
    
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 ();}}
   
  

It's not hard to explain.
The following is how to obtain WeatherActivity weather information:
The Code is as follows:

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 weather information GET method // HttpUtils. getWeatherGet (getIntent (). getStringExtra (theRedionCode); // obtain the weather information POST method // 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 );}}

I am a lazy here, just getting the weather information and displaying it in text.
This project implements three methods to obtain data, but does not describe how to obtain data in three ways. The following shows the code for obtaining data using http get and http post:

Public class HttpUtils {private static String TAG = HttpUtils. class. getSimpleName ();/*** obtain province * post method */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
  
   
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.exe cute (httpPost); if (httpResponse. getStatusLine (). getStatusCode () == 200) {String msg = EntityU Tils. toString (httpResponse. getEntity (); Log. I (TAG, MSG = + msg);} else {Log. I (TAG, failed to get);} catch (Exception e) {e. printStackTrace (); Log. I (TAG, get exception 1 );}}}. start ();}/*** GET province * GET Method */public static void GetProviceGet () {Log. I (TAG, HttpUtils = GetProviceGet); new Thread () {@ Override public void run () {super. run (); HttpClient httpClient = new DefaultHttpClient (); HttpGet httpGet = new Http Get (Constants. URI +/getRegionProvince ?); Try {HttpResponse httpResponse = httpClient.exe cute (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, get exception 2 );}}}. start ();}/*** GET city * GET Method */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.exe cute (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, get exception 2 );}}}. start ();}/*** get city * post method */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
   
    
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.exe cute (httpPost); if (httpResponse. getStatusLine (). getStatusCode () == 200) {String msg = EntityUtils. toString (httpResponse. getEntity (); Log. I (TAG, MSG = + msg);} else {Log. I (TAG, failed to get);} catch (Exception e) {e. printStackTrace (); Log. I (TAG, get exception 1 );}}}. start ();}/*** obtain weather information * post method */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
    
     
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); HttpResp Onse httpResponse = httpClient.exe cute (httpPost); if (httpResponse. getStatusLine (). getStatusCode () == 200) {String msg = EntityUtils. toString (httpResponse. getEntity (); Log. I (TAG, MSG = + msg);} else {Log. I (TAG, failed to get);} catch (Exception e) {e. printStackTrace (); Log. I (TAG, get exception 1 );}}}. start ();}/*** obtain weather information * GET Method */public static void GetWeatherGet (final String theCityCode) {Log. I (TAG, HttpU Tils = 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 = route cute (httpGet); HttpEntity httpEntity = httpResponse. getEntity (); if (response! = 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, get exception 2 );}}}. start ();}}
    
   
  

This class enables network access to weather information data. You may ask, this class does not parse the data, but simply prints the obtained data? In fact, the obtained data is the same as the first soap method.
 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.