KILON 2015-04-18 11:40 采纳率: 100%
浏览 2460
已采纳

android 调用 webservice 返回字符串想在listview中显示。出现问题……

下文是 主查询模块 Query_xiaofeijilu.java,

package com.gongneng.chaxun;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;



import org.json.JSONException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import com.constant.Constant;
import com.example.kehuduan.R;

public class Query_xiaofeijilu extends Activity {

    private Button okButton;
    private ListView listview;
    private Handler handler = null;
    private SimpleAdapter adapter;
    private List<Map<String,Object>> muldata;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.query_xiaofeijilu);
        okButton = (Button) this.findViewById(R.id.xiaofeijiluchaxun);
        listview = (ListView) this.findViewById(R.id.listxiaofeijilu);

        handler = new Handler();
        okButton.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread() {
                    @Override
                    public void run() {

                        try {
                            try {
                                getWebservice();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (XmlPullParserException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        });
    }

    public void getWebservice() throws InterruptedException, IOException,
            XmlPullParserException, JSONException {
        // 创建属于主线程的handler

        HttpTransportSE ht = new HttpTransportSE(Constant.SERVICE_URL);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        SoapObject request = new SoapObject(Constant.SERVICE_NS,Constant.QueryAction_chaxunxiaofeijilu);
        request.addProperty("aac999", "2200000");
        envelope.bodyOut = request;// 往出走的数据Out,也就是服务器的传入值
        try {
            ht.call(null, envelope);
            Object object = (Object) envelope.getResponse();// 就用Object来取

            muldata = new ArrayList<Map<String,Object>>();

            if (envelope.getResponse() != null) {
                String a = object.toString();
                String b =  a.replace("[","");//变[为空
                String c =  b.replace("]","");
                String d =  c.replace("{","");
                String e =  d.replace("}","");
                String f = e;
//              String[] arr = c.split(",");//将字符串转为数组,以,为分割
                    Map<String, Object> aa = transStringToMap(f);
                    Iterator<?> iter = aa.entrySet().iterator(); 
                    while (iter.hasNext()) { 
                        Map<String,Object> map=new HashMap<String, Object>();
                        Map.Entry entry = (Map.Entry) iter.next(); 
                        Object key = entry.getKey(); 
                        Object val = entry.getValue(); 
                        map.put(key.toString(),val.toString());
//                      map.put("text", "测试文本:");
                        muldata.add(map);
                    }
                    adapter = buildListAdapter(this, muldata);

                    new Thread() {
                        public void run() {
                            handler.post(runnableUi);
                        }
                    }.start();
                    return;


            } else {
                System.out.println("没有返回值");
                return;
            }
        } catch (SoapFault e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    // 构建adapter.
    public SimpleAdapter buildListAdapter(Context context,List<Map<String, Object>> data) {
        SimpleAdapter adapter = new SimpleAdapter(context, data,
                R.layout.chaxunxiaofeijilulist, 
                new String[] { "AKB021","AKE038","KS","JS","ROWNUM" }, 
                new int[] { R.id.akb021, R.id.ake038,R.id.ks,R.id.js ,R.id.rownum});
        return adapter;
    }

    // 构建Runnable对象,在runnable中更新界面 // 更新页面list 需要单独写个线程
        Runnable runnableUi = new Runnable() {
        @Override
        public void run() {
            // 更新界面
            listview.setAdapter(adapter);
            Looper.loop();
        }
    };
    //===================================
    //字符串转MAP
      public static Map<String, Object> transStringToMap(String mapString){  
          Map<String, Object> map = new HashMap<String, Object>();  
          java.util.StringTokenizer items;  
          for(StringTokenizer entrys = new StringTokenizer(mapString, ",");
                  entrys.hasMoreTokens();   
            map.put(items.nextToken(), items.hasMoreTokens() ? ((Object) (items.nextToken())).toString() : null))  
              items = new StringTokenizer(entrys.nextToken(), ":");  
          return map;  
        }  



}

下文是list页面

 <?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/child"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <!-- 若想隐藏id加入此属性: android:visibility="gone",这样做有点类似于Html中hidden域      android:background="#E3D25E" -->

    <TextView
        android:id="@+id/akb021"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:textColor="#6633FF"
         android:text="   akb021   "
         />

    <TextView
        android:id="@+id/ake038"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="   ake038   "/>

    <TextView
        android:id="@+id/ks"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="   ks   "/>

    <TextView
        android:id="@+id/js"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="   js   "/>
    <TextView
        android:id="@+id/rownum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="   rownum   "/>

</LinearLayout>

主显示页面

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editTextShenfenzheng"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/listxiaofeijilu"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/listxiaofeijilu"
        android:ems="10"
        android:hint="@string/shenfenzheng" >

    </EditText>

    <Button
        android:id="@+id/chongzhi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="@string/chongzhi" />

    <ListView
        android:id="@+id/listxiaofeijilu"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/xiaofeijiluchaxun"
        android:layout_alignRight="@+id/chongzhi"
        android:layout_below="@+id/editTextShenfenzheng"
         >

    </ListView>

    <Button
        android:id="@+id/xiaofeijiluchaxun"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/chongzhi"
        android:layout_alignBottom="@+id/chongzhi"
        android:layout_toLeftOf="@+id/chongzhi"
        android:text="@string/xiaofeijilu" />

</RelativeLayout>

但是现在只把行数显示出来了,里边没有值,附图:

图片说明
这个问题,困扰了我好几天了,烦请各位好友,帮忙出出招,在此写过了!!!

  • 写回答

3条回答 默认 最新

  • danielinbiti 2015-04-18 12:20
    关注
    你往simpleadapter中有4个key"AKB021","AKE038","KS","JS","ROWNUM",但map只放1个
     while (iter.hasNext()) { 
                            Map<String,Object> map=new HashMap<String, Object>();
                            Map.Entry entry = (Map.Entry) iter.next(); 
                            Object key = entry.getKey(); 
                            Object val = entry.getValue(); 
                            map.put(key.toString(),val.toString());  //1个 应该map要放4个
    //                      map.put("text", "测试文本:");  //
                            muldata.add(map);
                        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。