Android与webservice之间的交互
前几天我给别人做了一个webservice与手机客户端进行交互的程序,光这个服务器端的搭建大约花了我一天的时间,现在将一些失误和收获跟大家分享一下。
- 失误一:我是用的eclipse版本不对,应该用eclipse-jee-ganymede-SR2-win32.zip
- 失误二:轻信网络上的一些网友的步骤,导致越陷越深,最终必须重来
- 失误三:我的客户端对wsdl不能正确读取;只能在使用web读取,而手机无法读取
上图为wdsl的展示界面,其中是一个四则运算的web服务,“a/b”是两个参数
如下是九宫格的程序及截图
九宫格package com.apps; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.SimpleAdapter; public class JiuGg extends Activity { /** Called when the activity is first created. */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); GridView gridview = (GridView) findViewById(R.id.GridView); ArrayList<HashMap<String, Object>> meumList = new ArrayList<HashMap<String, Object>>(); String name[] = { "Skype", "计算器", "花鸟", "写字板", "仪表", "天气", "字典", "视频", "音乐" }; int num[] = { R.drawable.b1, R.drawable.b2, R.drawable.b3, R.drawable.b4, R.drawable.b5, R.drawable.b6, R.drawable.b7, R.drawable.b8, R.drawable.b9 }; for (int i = 0; i < num.length; i++) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("ItemImage", num[i]); map.put("ItemText", name[i]); meumList.add(map); } SimpleAdapter saMenuItem = new SimpleAdapter(this, meumList, R.layout.meunitem, new String[] { "ItemImage", "ItemText" }, new int[] { R.id.ItemImage, R.id.ItemText }); gridview.setAdapter(saMenuItem); gridview.setOnItemClickListener(new ItemClickListener()); // { // public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, // long arg3) { // // System.out.println("click index:" + arg2); // Intent intent = new Intent(); // intent.setClass(JiuGg.this, Calculator.class); // //调用一个新的Activity // startActivity(intent); // //关闭原本的Activity // JiuGg.this.finish(); // // } // } // ); } class ItemClickListener implements OnItemClickListener { public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { System.out.println("click index:" + arg1); Intent intent = new Intent(); intent.setClass(JiuGg.this, Calculator.class); startActivity(intent); JiuGg.this.finish(); } } }
读取web服务package com.apps; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import android.R.integer; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Calculator extends Activity implements OnClickListener { private Button bt; private EditText et; private TextView textView; private static final String METHOD_NAME = "add"; private static final String NAMESPACE = "http://apps.com"; private static final String URL = "http://192.168.0.102:18802/XML/services/Calculator?wsdl"; private SoapObject resultsRequestSOAP = null; public void onClick(View view) { textView=(TextView)findViewById(R.id.textview); et = (EditText) findViewById(R.id.nums); String etString = et.getText().toString(); textView.setText(etString); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.calculator); bt = (Button) findViewById(R.id.calculator1); bt.setOnClickListener(this); } }
下图为最终的显示结果
posted on 2011-02-08 09:43 ggbond1988 阅读(2116) 评论(4) 编辑 收藏 举报