服务器向Android写一个对象

服务器端servlet:
Java code

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("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 code

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();
        }
    }

}



到了客户端
Java code

Albums = (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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值