求大神给个实例,搞了好几天了都没弄成功!
网上的例子大都是2.X版本的!放到4.0有问题!
有人说开线程,我试了还是不行!可能我技术问题!!
求大神给实例!
最好能调用http://www.webxml.com.cn/WebServices/WeatherWebService.asmx报个天气预报!!!

------解决方案--------------------
楼主看看这个吧http://blog.csdn.net/xiaanming/article/details/16871117
顺便帮忙戳一下吧http://vote.blog.csdn.net/blogstaritem/blogstar2013/xiaanming
------解决方案--------------------
public class MainActivity extends Activity {
private EditText text;
private Button btn;
public static String WeatherInfo = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) this.findViewById(R.id.editText1);
btn = (Button) this.findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
Service s=new Service();
s.start();
text.setText(WeatherInfo);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@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;
}
}
上面的是Activity里面的东东,很简单的,就一个本文框,一个按钮
下面这个很重要,4.0以上版本都要求在非主线程当中访问webservice,否则会报NetworkOnMainThreadException的错误
package com.example.service;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import com.example.webservice.MainActivity;
import android.util.Xml;
public class Service extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
//将解析的数据赋值给MainActivity
MainActivity.WeatherInfo=getRealIp();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String getRealIp() throws IOException {
String params = "theCityCode=长沙&theUserID=";//设置发送的参数
byte[] entity = params.getBytes();
try {
HttpURLConnection con = (HttpURLConnection) new URL("http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather").openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setConnectTimeout(500);
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
con.setRequestProperty("Content-Length",String.valueOf(entity.length));
con.getOutputStream().write(entity);
System.out.println(con.getResponseCode());
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
String result = getByteArrayFromInputStream(con.getInputStream());
return result;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//Pull解析返回的XML数据
public static String getByteArrayFromInputStream(InputStream ism)
throws Exception{
String result = null;
XmlPullParser parser=Xml.newPullParser();
parser.setInput(ism, "utf-8");
int event=parser.getEventType();
while(event != XmlPullParser.END_DOCUMENT)
{
switch(event)
{
case XmlPullParser.START_TAG:
if("string".equals(parser.getName()))
{
result+=parser.nextText();
}
break;
}
event=parser.next();
}
return result;
}
}

