#1、写一个帮助类DBUtil
方法callWS,访问Wb返回相对应的数据
/**
String namespace = “http://tempuri.org/”;//namespace
String Url = “http://47.92.68.57:8099/WebServices_Device_Management.asmx?WSDL”;
String methodName = “Select”;
有参数的时候:
Map<String, Object> params = new HashMap<>();
params.put(“username”, “admin”);
params.put(“password”, “123456”);
-
@param nameSpace WS的命名空间
-
@param methodName WS的方法名
-
@param wsdl WS的wsdl的完整路径名
-
@param params WS的方法所需要的参数
-
@return SoapObject对象
*/
public static SoapObject callWS(String nameSpace, String methodName,
String wsdl, Map<String, Object> params) {
final String SOAP_ACTION = nameSpace + methodName;
SoapObject soapObject = new SoapObject(nameSpace, methodName);if ((params != null) && (!params.isEmpty())) {
Iterator<Map.Entry<String, Object>> it = params.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Object> e = (Map.Entry<String, Object>) ((Iterator) it).next();
soapObject.addProperty(e.getKey(), e.getValue());
}
}SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER12);
envelope.bodyOut = soapObject;
// 兼容.NET开发的Web Service
envelope.dotNet = true;
envelope.encodingStyle=“UTF-8”;HttpTransportSE ht = new HttpTransportSE(wsdl);
try {
ht.getServiceConnection();
ht.call(SOAP_ACTION, envelope);
if (envelope.getResponse() != null) {
SoapObject result = (SoapObject) envelope.bodyIn;
return result;
} else {
return null;
}
} catch (Exception e) {
Log.e(“error”, e.getMessage());
}
return null;
}
方法二convertJSON2List 本案例Wb返回数据是Json格式
/**
-
@param result JSON字符串
-
@param name JSON数组名称
-
@param fields JSON字符串所包含的字段
-
@return 返回List<Map<String , Object>>类型的列表,Map<String,Object>对应于 “id”:"1"的结构
/
public static List<Map<String, Object>> convertJSON2List(String result,String name, String[] fields) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try {
JSONArray array = new JSONObject(result).getJSONArray(name);
for (int i = 0; i < array.length(); i++) {
JSONObject object = (JSONObject) array.opt(i);
Map<String, Object> map = new HashMap<String, Object>();
for (String str : fields) {
map.put(str, object.get(str));
}
list.add(map);
}
} catch (JSONException e) {
Log.e(“error”, e.getMessage());
}
return list;
}
#2在一个Avtivity中写一个点击事件
/*-
初始化数据
*/
private void init() {
loginAccount = (EditText) findViewById(R.id.login_account);
loginPassword = (EditText) findViewById(R.id.login_password);
loginBtn = (Button) findViewById(R.id.login_btn);
registerBtn = (Button) findViewById(R.id.register_btn);
//点击登录按钮
loginBtn.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) { DBUtil db = new DBUtil("1", "2");//调用数据库查询类 String namespace = "http://tempuri.org/";//namespace //String soap_action = "http://tempuri.org/Check_User"; String Url = "http://47.92.68.57:8099/WebServices_Device_Management.asmx?WSDL"; String methodName = "Select"; Map<String, Object> params = new HashMap<>(); params.put("username", "admin"); params.put("password", "123456"); SoapObject soapObject = db.callWS(namespace, methodName, Url, null); if (soapObject != null) { String detail = soapObject.getProperty("SelectResult").toString(); //Toast.makeText(LoginActivity.this, detail, Toast.LENGTH_SHORT).show(); try { //将JSON字符串转换为List的结构 List<Map<String, Object>> list = DBUtil.convertJSON2List( detail, "Result_List", new String[]{"id", "username", "password", "email"}); //通过Intent将List传入到新的Activity-----本案例GridViewTestActivity Intent newIntent = new Intent(LoginActivity.this, GridViewTestActivity.class); Bundle bundle = new Bundle(); //List一定是一个Serializable类型 bundle.putSerializable("key", (Serializable) list); newIntent.putExtras(bundle); //启动新的Activity startActivity(newIntent); } catch (Exception e) { e.printStackTrace(); } } else { System.out.println("This is null..."); } }
});
#三、在新的GridViewTestActivity中
public class GridViewTestActivity extends AppCompatActivity {
GridView grid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid_view_test);
Intent intent=this.getIntent();
Bundle bundle=intent.getExtras();
//获得传进来的List<Map<String,Object>>对象
@SuppressWarnings(“unchecked”)
List<Map<String, Object>> list = (List<Map<String, Object>>) bundle
.getSerializable(“key”);
Map<String, Object> l=list.get(0);
String s=l.get(“username”).toString();
//通过findViewById方法找到GridView对象
grid = (GridView) findViewById(R.id.grid01);
//SimpleAdapter适配器填充
//1.context 当前上下文,用this表示,或者GridViewTest.this
//2.data A List of Maps.要求是List<Map<String,Object>>结构的列表,即数据源
//3.resource 布局文件
//4.from 从哪里来,即提取数据源List中的哪些key
//5.to 到哪里去,即填充布局文件中的控件
SimpleAdapter adapter = new SimpleAdapter(this, list,
R.layout.list_item, new String[] { “id”, “username”,“password”,“email” },
new int[] { R.id.id, R.id.name, R.id.password,R.id.email });//将GridView和适配器绑定
grid.setAdapter(adapter);
}
}
四、GridViewTestActivity对应的布局文件
<GridView
android:id="@+id/grid01"
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:horizontalSpacing=“3pt”
android:verticalSpacing=“1pt”
android:numColumns=“4”android:gravity="center"/>
-
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
六、最终显示界面