伺服器端servlet:Java codepackage 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("123@126.com"); 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(); } }}用戶端原始碼:Java codepackage 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(); } }}到了用戶端Java codeAlbums = (List<Album>) ois.readObject(); Toast.makeText(this, Albums.toString(), Toast.LENGTH_LONG).show(); Toast.makeText(this, Albums.size(), Toast.LENGTH_LONG).show();
來自:http://topic.csdn.net/u/20110509/02/00f92e6b-af99-41c5-8f5b-1631f70aacbc.html?seed=1698896095&r=78813327#r_78813327