SoapObject 的使用,运行出错!

myh0st 发布于 2013/05/04 16:22
阅读 1K+
收藏 0
package com.example.skytest;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	//命名空间
    private static final String NAMESPACE="http://WebXml.com.cn/";
    //请求URL
    private static final String URL="http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx";
    //调用城市的方法(需要带参数)
    String METHOD_NAME="getWeatherbyCityName";
    String SOAP_ACTION = NAMESPACE + "getWeatherbyCityName";
    String cityname="成都";
    TextView textview1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		textview1 = (TextView) this.findViewById(R.id.showDetil);
		SoapObject rpc = new SoapObject(NAMESPACE,METHOD_NAME);
		rpc.addProperty("theCityName", cityname);
		HttpTransportSE ht = new HttpTransportSE(URL,10000);
		ht.debug = true;
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.bodyOut = rpc;
		envelope.dotNet = true;
		envelope.setOutputSoapObject(rpc);
		try {
			ht.call(SOAP_ACTION, envelope);//出错地方,大家看看为什么一运行到这里程序就终止了
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SoapObject result = (SoapObject) envelope.bodyIn;
		SoapObject detiles = (SoapObject)result.getProperty("getWeatherbyCityName");
		String detile = null;
		try {
			detile = parseWeather(detiles);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		textview1.setText(detile);
	}
	 private String parseWeather(SoapObject detail2) throws UnsupportedEncodingException {
		// TODO Auto-generated method stub
		 String date = detail2.getProperty(6).toString();
		 String weatherToday;
	     weatherToday = "今天:" + date.split(" ")[0];
	     weatherToday = weatherToday + "\n天气:" + date.split(" ")[1];
	     weatherToday = weatherToday + "\n气温:"
	                + detail2.getProperty(5).toString();
	     weatherToday = weatherToday + "\n风力:"
	                + detail2.getProperty(7).toString() + "\n";
	        System.out.println("weatherToday is " + weatherToday);
	        Toast.makeText(MainActivity.this, weatherToday, Toast.LENGTH_LONG)
	                .show();
	        return weatherToday;
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

单步调试到出错点就会返回一个错是: 希望大牛帮帮忙!

加载中
0
m
myh0st
问题原因找到了,貌似在模拟器上会出问题,在真的手机上可以用!
返回顶部
顶部