如何通过SOAP从ASMX获取无参JSON并绑定到ListView?代码修改后无效
我帮你梳理下可能的问题点和修正方案,你已经确认了JSON数据和网络访问没问题,那大概率是SOAP请求配置、AsyncTask的后续处理或者ListView绑定环节出了小问题,一步步来:
一、先补全SOAP请求的关键配置
因为是无参数的.NET ASMX服务请求,几个核心配置不能少:
- 必须把创建好的
SoapObject添加到信封里:envelope.setOutputSoapObject(request); - 务必开启
.dotNet标识:envelope.dotNet = true;(你代码里写了一半,这个是适配.NET SOAP服务的关键) - 要指定正确的SOAPAction和服务URL,还要开启调试方便排查:
@Override protected String doInBackground(String... params) { String resultedData = null; try { SoapObject request = new SoapObject("http://tempuri.org/", "TESTUSERJSON"); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(request); // 替换成你的ASMX服务完整URL,比如https://yourdomain.com/YourService.asmx HttpTransportSE httpTransport = new HttpTransportSE("你的ASMX服务地址"); httpTransport.debug = true; // 开启调试,方便查看请求响应日志 // SOAPAction要和服务端定义的一致,这里对应你的TESTUSERJSON方法 httpTransport.call("http://tempuri.org/TESTUSERJSON", envelope); // 获取服务返回的JSON字符串,这里假设服务直接返回SoapPrimitive类型的JSON SoapPrimitive response = (SoapPrimitive) envelope.getResponse(); resultedData = response.toString(); } catch (Exception e) { e.printStackTrace(); resultedData = null; } return resultedData; }
二、修正AsyncTask的UI更新逻辑
AsyncTask的doInBackground只能处理后台任务,绝对不能在这个方法里操作UI,必须在onPostExecute里处理JSON解析和ListView绑定:
@Override protected void onPostExecute(String result) { super.onPostExecute(result); if (result != null && !result.isEmpty()) { // 调用解析方法绑定数据到ListView bindDataToListView(result); } else { // 处理请求失败的情况,比如给用户弹个提示 Toast.makeText(getApplicationContext(), "请求数据失败", Toast.LENGTH_SHORT).show(); } }
三、JSON解析与ListView绑定的具体实现
假设你的返回JSON是用户数组格式(比如[{"ID":1,"UserName":"张三"},...]),先定义一个实体类User,再写解析和绑定逻辑:
// 先定义User实体类 public class User { private int id; private String userName; // Getter和Setter方法 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } } // 解析JSON并绑定到ListView的方法 private void bindDataToListView(String jsonStr) { try { JSONArray userArray = new JSONArray(jsonStr); List<User> userList = new ArrayList<>(); for (int i = 0; i < userArray.length(); i++) { JSONObject userObj = userArray.getJSONObject(i); User user = new User(); user.setId(userObj.getInt("ID")); user.setUserName(userObj.getString("UserName")); userList.add(user); } // 创建自定义适配器并绑定 UserListAdapter adapter = new UserListAdapter(this, userList); yourListView.setAdapter(adapter); } catch (JSONException e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "JSON解析失败", Toast.LENGTH_SHORT).show(); } } // 自定义ListView适配器 class UserListAdapter extends ArrayAdapter<User> { private Context context; private List<User> userList; public UserListAdapter(Context context, List<User> userList) { super(context, R.layout.list_item_user, userList); this.context = context; this.userList = userList; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.list_item_user, parent, false); } User user = userList.get(position); TextView tvId = convertView.findViewById(R.id.tv_id); TextView tvName = convertView.findViewById(R.id.tv_name); tvId.setText(String.valueOf(user.getId())); tvName.setText(user.getUserName()); return convertView; } }
这里的list_item_user是你自定义的列表项布局文件,里面包含显示用户信息的TextView。
四、额外排查要点
- 网络权限:确保
AndroidManifest.xml里添加了网络权限:<uses-permission android:name="android.permission.INTERNET" /> - 调试日志:开启
httpTransport.debug = true后,可以在doInBackground的catch块里打印httpTransport.requestDump和httpTransport.responseDump,查看请求是否正确发送、服务端返回的内容是否符合预期 - 线程检查:确认所有网络操作都在AsyncTask的
doInBackground里,没有在主线程执行网络请求
内容的提问来源于stack exchange,提问作者Comingsoon
相关产品推荐
相关产品推荐

