服务器端servlet:package sei.servlet;
import java.io.IOException;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import sei.vo.Album;import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;public class TestServlet extends HttpServlet {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;        // 为客户端的get请求作响应
        protected void doGet(HttpServletRequest request,    HttpServletResponse response) throws ServletException, IOException {
            List<Album> Albums = new ArrayList<Album>();
         Album album = new Album();
         album.setID("1");
         album.setName("xiaozhou");
         album.setEmail("[email protected]");
         Albums.add(album);
         sendObject(Albums,response);
        
        }
        
        protected void sendObject(Object obj, HttpServletResponse response)
        {
           try
           {  //Album album = new Album();
               OutputStream os = response.getOutputStream();
               ObjectOutputStream oos = new ObjectOutputStream(os);
               oos.writeObject(obj);
           }
           catch (Exception e)
           {
               e.printStackTrace();
           }
                    
}
}客户端源代码:package development.samples.helloAndroid;import java.io.InputStream;
import java.io.ObjectInputStream;import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;import src.service.Album;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;import java.util.ArrayList;
import java.util.List;
public class GetObject extends Activity {
@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mainobject);
TextView textI = (TextView) this.findViewById(R.id.id);
TextView textN = (TextView) this.findViewById(R.id.name);
TextView textE = (TextView) this.findViewById(R.id.email);
String httpUrl = "http://113.55.36.188:8080/Bisycle_Rent/test";
HttpGet httpRequest = new HttpGet(httpUrl);
HttpClient httpclient = new DefaultHttpClient();
try { HttpResponse res = httpclient.execute(httpRequest);
// Toast.makeText(this, "connected", Toast.LENGTH_LONG).show();
if (res.getStatusLine().getStatusCode() == 200) {
//Toast.makeText(this, "right", Toast.LENGTH_LONG).show();
InputStream is = res.getEntity().getContent();
if(is!=null){
//Toast.makeText(this, "right2", Toast.LENGTH_LONG).show();
ObjectInputStream ois = new ObjectInputStream(is);
//Toast.makeText(this, "right3", Toast.LENGTH_LONG).show();
//String b = "sad";
//b = (String)ois.readObject();
//Toast.makeText(this, is.toString(), Toast.LENGTH_LONG).show();
List<Album> Albums = new ArrayList<Album>();
Albums = (List<Album>) ois.readObject();
Toast.makeText(this, Albums.toString(), Toast.LENGTH_LONG).show();
Toast.makeText(this, Albums.size(), Toast.LENGTH_LONG).show();

/*Album album = new Album();
album = (Album)ois.readObject();
Toast.makeText(this, album.toString(), Toast.LENGTH_LONG).show();
ois.close();

is.close();
*/

//Toast.makeText(this, "right5", Toast.LENGTH_LONG).show();

//Toast.makeText(this, "right6", Toast.LENGTH_LONG).show();
//Iterator<Album> iter = list.iterator();


//Album album = (Album) list.get(1);
//Toast.makeText(this, album.getID(), Toast.LENGTH_LONG).show();
//textI.setText(album.getID());
//textN.setText(album.getName());
//textE.setText(album.getEmail());

}else{
Toast.makeText(this, "null", Toast.LENGTH_LONG).show();
} } else {
// Toast.makeText(this, "error", Toast.LENGTH_LONG).show();
textI.setText("请求错误");
}
} catch (Exception e) {
e.getStackTrace();
}
}}
到了客户端 
Albums = (List<Album>) ois.readObject();
Toast.makeText(this, Albums.toString(), Toast.LENGTH_LONG).show();
Toast.makeText(this, Albums.size(), Toast.LENGTH_LONG).show();中的第一个Albums.toString()读取出来的的确是[album1,album2],但到了Albums.size();就读取不出来了。
开始以为android SDK 版本不支持java.util.List.升级到 android 3.0还是不行在线求解

解决方案 »

  1.   

    貌似 list.size();放在 toast.setTextx(this, list.size(),Toast.LENGTH_LONG).show();
    中间那个参数位置 本来就读不出来 不过对象能传过来 哎 一叶遮目 不见泰山。我被这个方法骗的好惨我以为list对象传不过来,或者list在android SDK中有问题呢,其实只是一个读取参数错我而已其他都正常~!
      

  2.   

    大哥,那个list是没有问题的,你把代码改成toast.setTextx(this,“”+list.size(),Toast.LENGTH_LONG).show();就读得出来了