服务器端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

更多相关文章

  1. Android腾讯微薄客户端开发十三:提及篇(与我有关的微博)
  2. Android腾讯微薄客户端开发十二:博主的偶像
  3. Android腾讯微薄客户端开发十一:博主的粉丝
  4. [置顶] [转]android开发新浪微博客户端 完整攻略 [新手必读]
  5. 基于Android的淘宝网手机客户端开发见解(配完整视频教程讲解)
  6. Android实现远程控制PC(Android[客户端]+Qt[服务器端])

随机推荐

  1. android中的滚动条ScrollView
  2. Android引导页面去除白屏
  3. ClipboardManager android剪切板使用
  4. Android之复选框对话框
  5. android 使用Okhttp封装上传JSON格式数据
  6. 2013.04.16——— android 获取状态栏高
  7. android 第一次运行应用的引导界面
  8. Eclipse 中的Android 工程突然无法启动调
  9. Android基础--电话拨号器
  10. Ubuntu 12.04 安装 Tomcat8 遇到的问题