如何高效提取web service数据?

我是通过数组来传递数据的,一旦提取的数据有几百条时,速度很慢。求大神们告知有没有更好的方法。

这是取数据的代码:

public List> CompanyInfo(ArrayList searchwhere) {
{
//Vector result=new Vector();
List> list = new ArrayList>();
arraylist.clear();
brraylist.clear();
arraylist.add("OrderCode");
brraylist.add((String) searchwhere.get(0));
arraylist.add("CompanyName");
brraylist.add((String) searchwhere.get(1));
crraylist=con.GetWebServre("CompanyInfo", arraylist, brraylist);
if(crraylist!=null)
{

for (int j = 0; j < crraylist.size(); j += 4) {
HashMap hashMap = new HashMap();
hashMap.put("OrderCode", crraylist.get(j));
hashMap.put("ProductName_JP", crraylist.get(j + 1));
hashMap.put("ProductName_CN", crraylist.get(j + 2));
hashMap.put("OrderNum", crraylist.get(j + 3));
list.add(hashMap);
}
}

return list;

   }


这是链接web sercice 的代码

 package com.example.login_0508;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import android.annotation.SuppressLint;

import com.example.login_0508.R.string;

public class HttpCon {
//  public ArrayList<String> GetWebServre(String methodName, String string,
//          String string2) {
//      // TODO Auto-generated method stub
//      return null;
//  }
    public ArrayList<String> GetWebServre(String methodName,ArrayList<String> Parameters,ArrayList<String>ParValues)
    {


        ArrayList<String> Values=new ArrayList<String>();
    //String ServerUrl="http://10.0.2.2:4124/Service1.asmx";
        String ServerUrl="http://10.0.2.2:2444/Service1.asmx";
         //String soapAction="http://tempuri.org/LongUserId1";
        String soapAction="http://tempuri.org/"+methodName;
         String data="";
            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/\">";
                for ( int i = 0; i < Parameters.size(); i++) {
                    tps=Parameters.get(i).toString();
                    //设置该方法的参数为.net webService中的参数名称
                    vps=ParValues.get(i).toString();        
                    ts="<"+tps+">"+vps+"</"+tps+">";
                    mreakString=mreakString+ts;
                }
                mreakString=mreakString+"</"+methodName+">";
                /*
                +"<HelloWorld xmlns=\"http://tempuri.org/\">"
                +"<x>string11661</x>"
                +"<SF1>string111</SF1>"
                + "</HelloWorld>"
                */
                String soap2="</soap:Envelope>"; 
                String requestData=soap+mreakString+soap2;
                //System.out.println(requestData);

            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();
            InputStream inStream=con.getInputStream();

            //data=parser(inStream);
            //System.out.print("11");
                    Values= inputStreamtovaluelist(inStream,methodName);
                    //System.out.println(Values.size());
            return Values;

            }
            catch(Exception e)
            {
                System.out.print("2221");
                return null;
            }
    }
    public   ArrayList<String>   inputStreamtovaluelist  (InputStream   in,String MonthsName)   throws   IOException   { 
        StringBuffer   out   =   new   StringBuffer();
        String s1="";
        byte[]   b   =   new   byte[4096]; 
        ArrayList<String> Values=new ArrayList<String>();
        Values.clear();
        for  (int   n;   (n   =   in.read(b))   !=   -1;)   {
            if(s1=="")
            {
                s1=new  String(b,   0,   n);
            }
            else
            {
                s1=s1+new String (b,   0,   n);
            }
                out.append(s1);
        } 
        System.out.println(out);
        String[] s13=s1.split("><");
        String ifString=MonthsName+"Result";
        String TS="";
        String vs="";       
        Boolean getValueBoolean=false;
        for(int i=0;i<s13.length;i++){
            TS=s13[i];
            System.out.println(TS);
            int j,k,l;
            j=TS.indexOf(ifString);
            k=TS.lastIndexOf(ifString);

            if (j>=0)
            {
                System.out.println(j);
                if (getValueBoolean==false)
                {
                    getValueBoolean=true;
                }
                else {

                }           
                if ((j>=0)&&(k>j))
                {
                     System.out.println("FFF"+TS.lastIndexOf("/"+ifString));
                     //System.out.println(TS);
                     l=ifString.length()+1;
                     vs=TS.substring(j+l,k-2);
                     //System.out.println("fff"+vs);
                     Values.add(vs);
                     System.out.println("退出"+vs);
                    getValueBoolean=false;
                    return   Values; 
                }

            }
            if (TS.lastIndexOf("/"+ifString)>=0)
            {
                getValueBoolean=false;
                return   Values; 
            }
            if ((getValueBoolean)&&(TS.lastIndexOf("/"+ifString)<0)&&(j<0))
            {
                 k=TS.length();
                 //System.out.println(TS);
                 vs=TS.substring(7,k-8);
                 //System.out.println("f"+vs);
                 Values.add(vs);
            }

        }

        return   Values; 
}


}

这是对listview 赋值代码

public void Infoshow()
{

    ListView lv=(ListView)findViewById(R.id.listView1);
    searchwhere=this.getIntent().getExtras().getStringArrayList("searchwhere");
    list=new DB().CompanyInfo(searchwhere);

  SimpleAdapter adapter = new SimpleAdapter(
                 this, 
                //this,
                list, 
                R.layout.listviewitem,

                new String[] { "OrderCode", "ProductName_CN", "ProductName_JP" ,"OrderNum"}, 
                new int[] { R.id.txt_OrderCode, R.id.txt_ProductName_CN, R.id.txt_ProductName_JP , R.id.txt_OrderNum});

  lv.setAdapter(adapter);
}

1个回答

首先你要确定在哪个环节消耗的时间比较多,客户端,网络传输,服务端的响应,如果是客户端,你需要优化反序列化部分,如果是网络,试试传输前数据压缩下,如果是服务端,可能跟数据的准备有关系,看看如何把数据准备做得快些。

wg547308195
大师已去 虽然你的话对于我这个刚学android的小白很高深,但非常感谢你给了我一个努力方向
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐