В настоящее время я занимаюсь разработкой приложений для Android, которым необходимо вызывать список клиентов из веб-службы с помощью KSOAP2 и отображать этот список в виде реселлера. Я много искал, но, похоже, я не могу найти ответ. Чаще всего я нахожу ответ, использующий залп для анализа JSON на Android вместо KSOP2.
Это мой веб-сервис, созданный моим учителем
<string xmlns="http://tempuri.org/">
{ "Table1": [ { "id": "1", "name": "shawn", "code": "code1", "contact": "0199999990", "address": "address1" }, { "id": "2", "name": "din", "code": "code2", "contact": "555345345", "address": "address2" }, { "id": "3", "name": "fatin", "code": "code3", "contact": "0198888", "address": "address3" }, { "id": "4", "name": "sue", "code": "code4", "contact": "01766622552", "address": "address4" }, { "id": "5", "name": "teoh", "code": "code5", "contact": "013778899", "address": "address5" }, { "id": "6", "name": "jia", "code": "code6", "contact": "0107787171", "address": "address6" } ] }
Это мой код, который я пытался получить список от веб-службы
private final String URL = "http://192.168.0.88/erp/callskybiz.asmx";
private final String NAMESPACE = "http://tempuri.org/";
private boolean getCustomerList(String id, String name, String code, String contact, final String address) {
final String SOAP_ACTION = "http://tempuri.org/getCustomerList";
final String METHOD_NAME = "getCustomerList";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// Property which holds input parameters
PropertyInfo idPI = new PropertyInfo();
idPI.setName("id");
idPI.setValue(id);
idPI.setType(String.class);
request.addProperty(idPI);
PropertyInfo namePI = new PropertyInfo();
namePI.setName("name");
namePI.setValue(name);
namePI.setType(String.class);
request.addProperty(namePI);
PropertyInfo codePI = new PropertyInfo();
codePI.setName("code");
codePI.setValue(code);
codePI.setType(String.class);
request.addProperty(codePI);
PropertyInfo contactPI = new PropertyInfo();
contactPI.setName("contact");
contactPI.setValue(contact);
contactPI.setType(String.class);
request.addProperty(contactPI);
PropertyInfo addressPI = new PropertyInfo();
addressPI.setName("address");
addressPI.setValue(address);
addressPI.setType(String.class);
request.addProperty(addressPI);
//create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true; // Set this variable to true for
// compatibility with what seems to be the
// default encoding for .Net-Services.
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
String url = "http://192.168.0.88/erp/callskybiz.asmx/getCustomerList";
JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("Table1");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject cust = jsonArray.getJSONObject(i);
String id = cust.getString("id");
String name = cust.getString("name");
String code = cust.getString("code");
String contact = cust.getString("contact");
String address = cust.getString("address");
list.add(new view_customer(id, name, contact, address, code));
}
adapter = new AdapterCustomer(SelectCustomerActivity.this, list);
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mRequestQueue.add(jsonRequest);
} catch (Exception e) {
Log.e("Error : " , "Error on soapPrimitiveData() " + e.getMessage());
e.printStackTrace();
}
return false;
}
private class CustomerListTask extends AsyncTask<Void, Void, Boolean> {
protected void onPreExecute() {
}
protected Boolean doInBackground(final Void... unused) {
return getCustomerList(id, name, code, contact, address);
}
protected void onPostExecute(Boolean result) {
if (result) {
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}