【Android进阶】Android调用WebService的实现
最近想自己搞搞服务器,就从最简单的webservice开始吧
先上效果图
项目结构
开始贴代码,注释都有,有问题的请留言
MainActivity.java
package com.example.webservice; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; /** * WebService调用实例 * @author ZhaoKaiQiang * * Time:2014年3月11日 */ public class MainActivity extends Activity { private String result; private EditText x; private EditText y; private TextView tv; Handler handler = new Handler() { public void handleMessage(Message msg) { tv.setText(result); }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); x = (EditText) findViewById(R.id.x); y = (EditText) findViewById(R.id.y); tv = (TextView) findViewById(R.id.tv); } public void click(View view) { new Thread(new Runnable() { @Override public void run() { //获取返回值 result = get("Add", Integer.valueOf(x.getText().toString().trim()), Integer.valueOf(y.getText().toString().trim())); //发送message将结果更新 handler.sendEmptyMessage(1); } }).start(); } /** * 调用WebService * @param Method 方法名 * @param x 加数x * @param y 加数y * @return 相加后的结果 */ public String get(String Method, int x, int y) { //命名空间 String NAME_SPACE = "http://tempuri.org/"; //Webservice的调用地址url String WEBSERVICE_URL = "http://119.167.225.206:55555/WebService1.asmx"; String data = "0"; try { String soapAction = NAME_SPACE + Method; SoapObject request = new SoapObject(NAME_SPACE, Method); // 添加方法需要的参数 request.addProperty("x", x); request.addProperty("y", y); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.dotNet = true; envelope.bodyOut = request; HttpTransportSE ht = new HttpTransportSE(WEBSERVICE_URL); ht.call(soapAction, envelope); if (envelope.getResponse() != null) { Object response = (Object) envelope.getResponse(); //data就是服务器返回的数据 data = response.toString().trim(); } } catch (Exception e) { Toast.makeText(MainActivity.this,e.getMessage(), 0).show(); } return data; } }
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="X" android:textSize="22sp" /> <EditText android:id="@+id/x" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Y" android:textSize="22sp" /> <EditText android:id="@+id/y" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="click" android:text="调用WebService" /> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="22sp" /> </LinearLayout>
最后注意权限,加上上网权限
代码里的服务器是暂时的,可能会失效,把里面的属性换成自己的就可以,有问题,请留言