package android.weather.ui;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.weather.domain.CallWebService;
import android.weather.domain.WebServiceUtil;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MyWeatherActivity extends Activity {
private CallWebService service;
private EditText etCityName;
private TextView tvShowResult;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
final String namespace = "http://WebXml.com.cn/";
final String url = "http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webservice);
etCityName = (EditText) this.findViewById(R.id.etCityName);
tvShowResult = (TextView) this.findViewById(R.id.textView1);
button = (Button) this.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String cityName = etCityName.getText().toString();
service = new WebServiceUtil(namespace,url);
service.callWebservice("getWeatherbyCityName", cityName);
String reString = service.handleWebServiceResult();
tvShowResult.setText(reString);
}
});
}
}
package android.weather.domain;
import org.ksoap2.serialization.SoapObject;
public interface CallWebService {
public SoapObject callWebservice(String methodName,String cityName);
public String handleWebServiceResult();
}
package android.weather.domain;
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
public class WebServiceUtil implements CallWebService {
String namespace ;
String url ;
public WebServiceUtil(String namespace, String url) {
this.namespace = namespace;
this.url = url;
}
SoapObject ss = null;
public SoapObject callWebservice(String methodName,String cityName) {
SoapObject soapObject = new SoapObject(namespace, methodName);
soapObject.addProperty("theCityName", cityName);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = soapObject;
envelope.dotNet = true ;
HttpTransportSE httpTransportSE = new HttpTransportSE(url);
httpTransportSE.debug = true ;
try {
httpTransportSE.call(namespace+methodName, envelope);
if( envelope.getResponse() != null ) {
SoapObject resultObject = (SoapObject) envelope.bodyIn;
ss = (SoapObject) resultObject.getProperty(methodName+"Result");
setSs(ss);
return ss;
}
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return ss;
}
@Override
public String handleWebServiceResult() {
String resultString = ss.toString();
return resultString;
}
public SoapObject getSs() {
return ss;
}
public void setSs(SoapObject ss) {
this.ss = ss;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/TextView"
android:textSize="23sp" />
<EditText
android:id="@+id/etCityName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/inputCityName"
android:textSize="23sp" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/showResult"
android:textSize="23sp" />
</LinearLayout>
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.weather.domain.CallWebService;
import android.weather.domain.WebServiceUtil;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MyWeatherActivity extends Activity {
private CallWebService service;
private EditText etCityName;
private TextView tvShowResult;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
final String namespace = "http://WebXml.com.cn/";
final String url = "http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webservice);
etCityName = (EditText) this.findViewById(R.id.etCityName);
tvShowResult = (TextView) this.findViewById(R.id.textView1);
button = (Button) this.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String cityName = etCityName.getText().toString();
service = new WebServiceUtil(namespace,url);
service.callWebservice("getWeatherbyCityName", cityName);
String reString = service.handleWebServiceResult();
tvShowResult.setText(reString);
}
});
}
}
package android.weather.domain;
import org.ksoap2.serialization.SoapObject;
public interface CallWebService {
public SoapObject callWebservice(String methodName,String cityName);
public String handleWebServiceResult();
}
package android.weather.domain;
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
public class WebServiceUtil implements CallWebService {
String namespace ;
String url ;
public WebServiceUtil(String namespace, String url) {
this.namespace = namespace;
this.url = url;
}
SoapObject ss = null;
public SoapObject callWebservice(String methodName,String cityName) {
SoapObject soapObject = new SoapObject(namespace, methodName);
soapObject.addProperty("theCityName", cityName);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = soapObject;
envelope.dotNet = true ;
HttpTransportSE httpTransportSE = new HttpTransportSE(url);
httpTransportSE.debug = true ;
try {
httpTransportSE.call(namespace+methodName, envelope);
if( envelope.getResponse() != null ) {
SoapObject resultObject = (SoapObject) envelope.bodyIn;
ss = (SoapObject) resultObject.getProperty(methodName+"Result");
setSs(ss);
return ss;
}
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return ss;
}
@Override
public String handleWebServiceResult() {
String resultString = ss.toString();
return resultString;
}
public SoapObject getSs() {
return ss;
}
public void setSs(SoapObject ss) {
this.ss = ss;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/TextView"
android:textSize="23sp" />
<EditText
android:id="@+id/etCityName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/inputCityName"
android:textSize="23sp" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/showResult"
android:textSize="23sp" />
</LinearLayout>