伺服器向Android寫一個對象

來源:互聯網
上載者:User
伺服器端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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.