android调用webservice解析Gson数据

Android端代码:

public class CheckActivity extends ActionBarActivity implements View.OnClickListener{

final static String NAME_SPACE = "http://tempuri.org/";
final static String SERVICE_URL = "http://192.168.0.174:8013/Roadflow.asmx";
// 调用的方法
String methodName = "getTask";
String soapAction = "http://tempuri.org/getTask";
EditText edit_1,edit_2;
String on;
String wo;
ListView listView;
List list;
MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_check);

viewInit();
get();
}
private void viewInit() {
// TODO Auto-generated method stub
listView = (ListView) findViewById(R.id.listView1);
edit_1 = (EditText) findViewById(R.id.edit_1);
edit_2 = (EditText) findViewById(R.id.edit_2);
list = new ArrayList();
adapter=new MyAdapter(CheckActivity.this, list);
listView.setAdapter(adapter);
TextView textView=(TextView) findViewById(R.id.button1);
textView.setOnClickListener(this);

}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
list.clear();
on = edit_1.getText().toString().trim();
wo = edit_2.getText().toString().trim();
get();
}


private void get() {


new AsyncTask() {


@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}


@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub


// 使用soap1.1协议创建Envelop对象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
// 实例化SoapObject对象
SoapObject request = new SoapObject(NAME_SPACE, methodName);
/**
* 设置参数,参数名不一定需要跟调用的服务器端的参数名相同,只需要对应的顺序相同即可
* */
request.addProperty("strID", on);
request.addProperty("strStatus", wo);
// 将SoapObject对象设置为SoapSerializationEnvelope对象的传出SOAP消息
envelope.dotNet = true;
envelope.bodyOut = request;
envelope.setOutputSoapObject(request);
new MarshalBase64().register(envelope);
// 创建httpTransportSE传输对象
HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
try {
// 调用webService
ht.call(soapAction, envelope);

if (envelope.getResponse() != null) {

/*************************************第一种解析*********************/

// Object result=(SoapPrimitive)envelope.getResponse();
// Object result=envelope.getResponse();
// String str=(String)result.toString();
// //ds父对象
// JSONArray jsonArray =new JSONObject(str).getJSONArray("RoadFlow_Task");
// int count=jsonArray.length();
//
//        for(int index=0;index //                            RoadFlowTaskBean bean=new RoadFlowTaskBean();
//                            
//             String ID=jsonArray.optJSONObject(index).getString("ID").toString();//获得数组里的对象,再根据对象获得字段
//             String Title=jsonArray.optJSONObject(index).getString("Title").toString();//获得数组里的对象,再根据对象获得字段
//             String StepName=jsonArray.optJSONObject(index).getString("StepName").toString();//获得数组里的对象,再根据对象获得字段
//             String SenderName=jsonArray.optJSONObject(index).getString("SenderName").toString();//获得数组里的对象,再根据对象获得字段
//         
//            // String ID1=jsonArray.getJSONObject(index).getString("ID").toString();//获得数组里的对象,再根据对象获得字段
//             
//             bean.setID(ID);
//             bean.setTitle(Title);
//             bean.setStepName(StepName);
//             bean.setSenderName(SenderName);
//             list.add(bean);
//            
//             System.out.println("----------------------"+ID);
//         }
      

// } else {

/*************************************第二种解析*********************/

Object result=(SoapPrimitive)envelope.getResponse();
JSONObject obj = new JSONObject(result.toString());
JSONArray jsonArray = (JSONArray)obj.getJSONArray("RoadFlow_Task");

for (int i = 0; i < jsonArray.length(); i++) {
JSONObject json = jsonArray.getJSONObject(i);
TestDTO.RoadFlowTaskBean test= new TestDTO.RoadFlowTaskBean();
test.setID(json.getString("ID"));
test.setTitle(json.getString("Title"));
list.add(test);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}


// 数据显示
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
adapter.notifyDataSetChanged();
}


}.execute();


}
}

你可能感兴趣的:(android调用webservice解析Gson数据)