首页 新闻 会员 周边 捐助

大神们帮忙看看啊,与服务器数据交互问题

0
悬赏园豆:20 [待解决问题]

程序就是从服务器webservice获取字符串
代码如下1.连接服务器

Java code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
public class httpcon{
    public ArrayList<String>GetWebServer(String methodName,ArrayList<String> Parameters,ArrayList<String> ParaValues){
 //soap传输
        ArrayList<String> Values =new ArrayList<String>();
        String ServerUrl="192.168.1.3:10001";
        String soapAction ="http://tempuri.org/" + methodName;
        String soap ="<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                "<soap:Body />";
        String tps, vps, ts;
        String mreakString = "";
        mreakString = "<" + methodName + " xmlns=\"http://tempuri.org/\">";
        System.out.println("联网前"+methodName);
        for(int i =0; i < Parameters.size(); i++)
       {
            tps = Parameters.get(i).toString();
            vps = ParaValues.get(i).toString();
            ts="<" + tps + ">" + vps + "</" + tps + ">";
            mreakString = mreakString +ts;}
            mreakString = mreakString + "</" + methodName + ">";
            String soap2 = "</soap:Envelope>";
            String requestData = soap + mreakString + soap2;
               //联网,数据交互
        try{URL url = new URL(ServerUrl);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            byte[] bytes = requestData.getBytes("utf-8");
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setUseCaches(false);
            con.setConnectTimeout(6000);// 设置超时时间
            con.setRequestMethod("POST");
            con.setRequestProperty("Content-Type""text/xml;charset=utf-8");
            con.setRequestProperty("SOAPAction", soapAction);
            con.setRequestProperty("Content-Length"""+bytes.length);
            OutputStream outStream = con.getOutputStream();
            outStream.write(bytes);
            outStream.flush();
            outStream.close();
            System.out.println("FFF"+methodName);
            System.out.println("FFF"+Parameters);
            InputStream inStream = con.getInputStream();
            StringBuffer out = new StringBuffer();
            String s1 = "";
                          //读取输出流
            byte[] b = new byte[4096];
            for (int n; (n = inStream.read(b)) != -1;) {
                s1 = new String(b, 0, n);
                out.append(s1);}
            System.out.println("FFFout"+out.toString());
            System.out.println("FFFout"+s1.toString());
                       //字符串转化为流,便于用pll解析器解析
            byte[] bytes1 = s1.getBytes() ;
            ByteArrayInputStream bais=new ByteArrayInputStream(s1.getBytes());
            Values = jsonxml(bais, methodName);
            return Values;
        catch (Exception e) {
            <span style="color: #FF0000;">System.out.print("2221");</span>
            return null;}}
    <span style="color: #993300;">public ArrayList<String> jsonxml(InputStream bais, String methodName) throws IOException</span> {                 // pull解析器解析所获数据流
           ArrayList<String> Values = new ArrayList<String>();
            Values.clear();
            XmlPullParser pullParser=Xml.newPullParser();
            try {
                 pullParser.setInput(bais ,"UTF-8");  //格式
                    int eventCode=pullParser.getEventType();//读取文档的开始
                    while(eventCode!=XmlPullParser.END_DOCUMENT) 
                   {
                     switch(eventCode){       
                        case XmlPullParser.START_DOCUMENT:break;
                         case XmlPullParser.START_TAG://判断是否为开始元素事件
                            if("string".equals(pullParser.getName()))
                              //判断开始元素是否为<string>,获取数据除soap固有格式外,都是<string>数据</string>
                               Values.add(pullParser.nextText());
 
                               }
                              ;break;
                        case XmlPullParser.END_TAG:break;   
                     }
                        eventCode= pullParser.next();//进入到下一个元素;'
                  }   
                  }
                  catch (XmlPullParserException e) 
                  {
                    e.printStackTrace();
                    }
                return Values;
                    }}


2.逻辑实现:

Java code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  public List<HashMap<String, String>> chakan(String Ztz) {
        List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
        arrayList.clear();
        brrayList.clear();
        crrayList.clear() ;
        arrayList.add("Ztz");
        brrayList.add(Ztz);
        crrayList = http.GetWebServer("gc", arrayList, brrayList);
        System.out.println("22222"+crrayList); 
        HashMap<String, String> tempHash = new HashMap<String, String>();
        if(crrayList!=null){
        for(int j = 0; j < crrayList.size(); j+=1) {
            HashMap<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("biaoti", crrayList.get(j));
            list.add(hashMap);
        }}
        System.out.println("111111"+list);
        System.out.println("chakan"+list);
    return list;
    }
   public List<HashMap<String, String>> ejym01(String Ztz) {
        List<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
        arrayList.clear();
        brrayList.clear();
        crrayList.clear() ;
        arrayList.add("Ztz");
        brrayList.add(Ztz);
        System.out.println("这是"+brrayList);
        crrayList = http.GetWebServer("ejymwt", arrayList, brrayList);
        System.out.println("crraylist"+crrayList); 
        if(crrayList!=null){
        for(int j = 0; j < crrayList.size(); j+=1) {
            HashMap<String, String> hashMap = new HashMap<String, String>();
            hashMap.clear();
            hashMap.put("biaoti", crrayList.get(j));
            list.add(hashMap);}}
        else{System.out.println("物质");}
        System.out.println("list"+list);
        return list;}
   


3.显示层

Java code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//这是一条查询
//
 runable run1=new runable();
Thread  thread1  = new Thread(run1);
 thread1.start();
 public  class runable implements Runnable{
         @Override
        public void run() {
                  System.out.println("本地OK!!");   
                 System.out.println("本地OK!!   当前线程"+Thread.currentThread().getName().toString());   
                  dbutil_1=new dbUtil();
                  List<HashMap<String, String>> list1 = new ArrayList<HashMap<String, String>>();
                  list1.clear();
                  list1 = dbutil_1.ejym01("市长信箱");
                   System.out.println("0000000"+list1);                           
                   adapter = new SimpleAdapter(
                          ej01.this
                          list1, 
                          R.layout.lv02, 
                          new String[]{"biaoti"}, 
                          new int[]{R.id.stv00});  
                    slv.post(new Runnable(){
                    public void run(){
                      slv.setAdapter(adapter);
                     int totalHeight = 0;  
                   for (int i = 0; i < adapter.getCount(); i++) {  
                       View listItem = adapter.getView(i, null, slv);  
                       listItem.measure(00);  
                       totalHeight += listItem.getMeasuredHeight();  
                   }  
                   ViewGroup.LayoutParams params = slv.getLayoutParams();  
                   params.height = totalHeight + (slv.getDividerHeight() * (slv.getCount() - 1));  
                   slv.setLayoutParams(params); 
                    System.out.println("本地测验");   
                  }});}


//其他是类似结构
//在2.2版本模拟器上运行时(连接服务器一层代码完全相同),数据完全正常显示
//在用jsonxml解析字符串前,用的另一种方式解析,结果是能获取数据,但是不稳定,在多条数据获取时往往显示不全; 换了jsonxml解析后,无法获取数据。断点捕获数据交互层异常,输出2221。

//也输出Skipped 182 frames!  The application may be doing too much work on its main thread.    但我分线程了啊,断点输出当前线程名字也不是主线程,难道非得用handler,post(runable)也不行?
//请教大神们,这到底是怎么回事儿?哪的问题啊?
//

日清日高的主页 日清日高 | 初学一级 | 园豆:4
提问于:2013-04-11 09:37
< >
分享
所有回答(1)
0

太长了

zhi++ | 园豆:487 (菜鸟二级) | 2013-04-11 19:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册