您的位置:首页 > 移动开发 > Android开发

Android访问WebService

2010-12-17 11:11 288 查看
Android调用Webservice实现手机与PCSERVER的交互,废话不多说,直接贴代码,下面是一段通过WEBSERVICE获取天气的代码

package com.android;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
private static String LOG_TAG = "Weather";
private static boolean DEBUG = false;
private static final int SHOW_ABOUT = 0x0001;
private static final String NAMESPACE = "http://WebXml.com.cn/";
//WebService地址
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/getWeatherbyCityName";

private String weatherToday;
private String weatherTomorrow;
private String weatherAfterday;
private String weatherCurrent;
private int iconToday[] = new int[2];
private int iconTomorrow[] = new int[2];
private int iconAfterday[] = new int[2];

private Button okButton;
private EditText textInput;
private ImageView imageView1;
private ImageView imageView2;
private TextView textWeatherToday;
private ImageView imageView3;
private ImageView imageView4;
private TextView textWeatherTomorrow;
private ImageView imageView5;
private ImageView imageView6;
private TextView textWeatherAfterday;
private TextView textWeatherCurrent;
private SoapObject detail;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

okButton = (Button)  findViewById(R.id.WeatherSearch);
textInput = (EditText) findViewById(R.id.TextWeather);
imageView1 = (ImageView) findViewById(R.id.ImageView01);
imageView2 = (ImageView) findViewById(R.id.ImageView02);
textWeatherToday = (TextView) findViewById(R.id.WeatherToday);
imageView3 = (ImageView) findViewById(R.id.ImageView03);
imageView4 = (ImageView) findViewById(R.id.ImageView04);
textWeatherTomorrow = (TextView) findViewById(R.id.WeatherTomorrow);
imageView5 = (ImageView) findViewById(R.id.ImageView05);
imageView6 = (ImageView) findViewById(R.id.ImageView06);
textWeatherAfterday = (TextView) findViewById(R.id.WeatherAfterday);
textWeatherCurrent = (TextView) findViewById(R.id.WeatherCurrent);

okButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
showWeather();
}
});
}

private void showWeather() {
String city = textInput.getText().toString();
if (city.length() == 0) city = "合肥";
getWeather(city);

textWeatherToday.setText(getWeatherToday());
imageView1.setImageResource(getIconToday(0));
imageView2.setImageResource(getIconToday(1));

textWeatherTomorrow.setText(getWeatherTomorrow());
imageView3.setImageResource(getIconTomorrow(0));
imageView4.setImageResource(getIconTomorrow(1));

textWeatherAfterday.setText(getWeatherAfterday());
imageView5.setImageResource(getIconAfterday(0));
imageView6.setImageResource(getIconAfterday(1));

textWeatherCurrent.setText(getWeatherCurrent());
}

public void getWeather(String cityName) {
try {
SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
rpc.addProperty("theCityName", cityName);

AndroidHttpTransport ht = new AndroidHttpTransport(URL);
ht.debug = true;

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);

envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.setOutputSoapObject(rpc);

ht.call(SOAP_ACTION, envelope);

debug(LOG_TAG, "DUMP>> " + ht.requestDump);
debug(LOG_TAG, "DUMP<< " + ht.responseDump);

SoapObject result = (SoapObject) envelope.bodyIn;
detail = (SoapObject) result.getProperty("getWeatherbyCityNameResult");

parseWeather(detail);
return;
} catch (Exception e) {
e.printStackTrace();
}
}

private void parseWeather(SoapObject detail) {
String date = detail.getProperty(6).toString();
weatherToday = "今天:" + date.split(" ")[0];
weatherToday = weatherToday + "/n天气:" + date.split(" ")[1];
weatherToday = weatherToday + "/n气温:" + detail.getProperty(5).toString();
weatherToday = weatherToday + "/n风力:" + detail.getProperty(7).toString() + "/n";
iconToday[0] = parseIcon(detail.getProperty(8).toString());
iconToday[1] = parseIcon(detail.getProperty(9).toString());

weatherCurrent = detail.getProperty(10).toString();

date = detail.getProperty(13).toString();
weatherTomorrow = "明天:" + date.split(" ")[0];
weatherTomorrow = weatherTomorrow + "/n天气:" + date.split(" ")[1];
weatherTomorrow = weatherTomorrow + "/n气温:" + detail.getProperty(12).toString();
weatherTomorrow = weatherTomorrow + "/n风力:" + detail.getProperty(14).toString() + "/n";
iconTomorrow[0] = parseIcon(detail.getProperty(15).toString());
iconTomorrow[1] = parseIcon(detail.getProperty(16).toString());

date = detail.getProperty(18).toString();
weatherAfterday = "后天:" + date.split(" ")[0];
weatherAfterday = weatherAfterday + "/n天气:" + date.split(" ")[1];
weatherAfterday = weatherAfterday + "/n气温:" + detail.getProperty(17).toString();
weatherAfterday = weatherAfterday + "/n风力:" + detail.getProperty(19).toString() + "/n";
iconAfterday[0] = parseIcon(detail.getProperty(20).toString());
iconAfterday[1] = parseIcon(detail.getProperty(21).toString());
}

public String getWeatherToday() {
debug(LOG_TAG, "weatherToday: " + weatherToday);
return weatherToday;
}

public String getWeatherTomorrow() {
debug(LOG_TAG, "weatherTomorrow: " + weatherTomorrow);
return weatherTomorrow;
}

public String getWeatherAfterday() {
debug(LOG_TAG, "weatherAfterday: " + weatherAfterday);
return weatherAfterday;
}

public String getWeatherCurrent() {
debug(LOG_TAG, "weatherCurrent: " + weatherCurrent);
return weatherCurrent;
}

public int getIconToday (int index) {
return iconToday[index];
}

public int getIconTomorrow (int index) {
return iconTomorrow[index];
}

public int getIconAfterday (int index) {
return iconAfterday[index];
}

private int parseIcon(String strIcon) {
if (strIcon == null) return -1;

if ("0.gif".equals(strIcon)) return R.drawable.a_0;
if ("1.gif".equals(strIcon)) return R.drawable.a_1;
if ("2.gif".equals(strIcon)) return R.drawable.a_2;
if ("3.gif".equals(strIcon)) return R.drawable.a_3;
if ("4.gif".equals(strIcon)) return R.drawable.a_4;
if ("5.gif".equals(strIcon)) return R.drawable.a_5;
if ("6.gif".equals(strIcon)) return R.drawable.a_6;
if ("7.gif".equals(strIcon)) return R.drawable.a_7;
if ("8.gif".equals(strIcon)) return R.drawable.a_8;
if ("9.gif".equals(strIcon)) return R.drawable.a_9;
if ("10.gif".equals(strIcon)) return R.drawable.a_10;
if ("11.gif".equals(strIcon)) return R.drawable.a_11;
if ("12.gif".equals(strIcon)) return R.drawable.a_12;
if ("13.gif".equals(strIcon)) return R.drawable.a_13;
if ("14.gif".equals(strIcon)) return R.drawable.a_14;
if ("15.gif".equals(strIcon)) return R.drawable.a_15;
if ("16.gif".equals(strIcon)) return R.drawable.a_16;
if ("17.gif".equals(strIcon)) return R.drawable.a_17;
if ("18.gif".equals(strIcon)) return R.drawable.a_18;
if ("19.gif".equals(strIcon)) return R.drawable.a_19;
if ("20.gif".equals(strIcon)) return R.drawable.a_20;
if ("21.gif".equals(strIcon)) return R.drawable.a_21;
if ("22.gif".equals(strIcon)) return R.drawable.a_22;
if ("23.gif".equals(strIcon)) return R.drawable.a_23;
if ("24.gif".equals(strIcon)) return R.drawable.a_24;
if ("25.gif".equals(strIcon)) return R.drawable.a_25;
if ("26.gif".equals(strIcon)) return R.drawable.a_26;
if ("27.gif".equals(strIcon)) return R.drawable.a_27;
if ("28.gif".equals(strIcon)) return R.drawable.a_28;
if ("29.gif".equals(strIcon)) return R.drawable.a_29;
if ("30.gif".equals(strIcon)) return R.drawable.a_30;
if ("31.gif".equals(strIcon)) return R.drawable.a_31;

return 0;
}

private static void debug(String tag, String msg) {
if (DEBUG) Log.d(tag, msg);
}

private void showAbout() {
TextView textAbout = new TextView(this);
textAbout.setText(R.string.about_text);
textAbout.setMovementMethod(LinkMovementMethod.getInstance());

Dialog dlg = new AlertDialog.Builder(this)
.setTitle(R.string.app_about)
.setView(textAbout)
.setPositiveButton(R.string.about_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.create();
dlg.show();
}
private void about_city(){

String result_city=detail.getProperty(11).toString();
new AlertDialog.Builder(this).setTitle(textInput.getText().toString()).setMessage(result_city).setPositiveButton("OK", null).show();

}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);

menu.add(0, SHOW_ABOUT, 0, R.string.app_about);

menu.add(0,2,1,R.string.city);

//        menu.addSubMenu("jack").add("tom1")
//        .add("tom2");
//menu.addS
this.getMenuInflater().inflate(R.layout.menu, menu);
return true;

}

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case SHOW_ABOUT:
showAbout();
break;
case 2:about_city();
break;
}
return true;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: