android http通訊

來源:互聯網
上載者:User

標籤:android   http   

***1.伺服器端
最簡單的伺服器端接收代碼,直接把穿過倆的資料列印*

package cn.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class MyServlet */@WebServlet("/MyServlet")public class MyServlet extends HttpServlet {    private static final long serialVersionUID = 1L;    /**     * @see HttpServlet#HttpServlet()     */    public MyServlet() {        super();        // TODO Auto-generated constructor stub    }    /**     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)     */    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub        //堅持調用post方法        this.doPost(request, response);    }    /**     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)     */    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub        //獲得資料        String name = request.getParameter("name");        String age = request.getParameter("age");        //列印資料        System.out.println(name);        System.out.println(age);    }}

sevlet


MyServlet
cn.servlet.MyServlet


MyServlet
/ss

2.用戶端
首先我們自訂一個線程

public class Thread1 extends Thread//資料來自布局的文本編輯框String name ;    String age;    String url;    public Thread1(String name, String age,String url){        this.name=name;        this.age=age;        this.url=url;    }

1)get方法的實現

public void doGet(){        try {        //get方法的特別注意的地方            url=url+"?name="+URLEncoder.encode(name, "utf-8")+"&age="+age;            //轉換為url對象            URL httpUrl = new URL(url);            //擷取連線物件                HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();                //設定傳輸方法                conn.setRequestMethod("GET");                conn.setReadTimeout(5000);                //讀取輸入資料流                BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));                String str;                StringBuffer sb = new StringBuffer();                //分別放入sb                while((str=br.readLine())!=null){                    sb.append(str);                }        } catch (MalformedURLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }

2)post方法的實現

public void doPost(){        try {            URL httpUrl = new URL(url);            HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();            conn.setRequestMethod("POST");            conn.setReadTimeout(5000);            //注意這裡是conn.getoutputstream,而不是new出來的,這樣才能關聯起來            OutputStream out =conn.getOutputStream();            String content = "name="+name+"&age="+age;            out.write(content.getBytes());            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));            String str;            StringBuffer sb = new StringBuffer();            while((str=br.readLine())!=null){                sb.append(str);            }        } catch (MalformedURLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }

在run方法裡面執行的是哪個方法就使用相應的方式

@Override    public void run() {        // TODO Auto-generated method stub        super.run();    doGet();//      doPost();    }

3)用戶端輸入資料的布局簡單布置了一下就是兩個編輯文字框和一個按鈕

private EditText etName,etAge;    private Button btnReg;

4)按鈕的點擊事件

 btnReg.setOnClickListener(new OnClickListener() {            public void onClick(View arg0) {                // TODO Auto-generated method stub                //把ip地址改為自己目前的                String url = "http://192.168.191.1:8080/serve/ss";                new Thread1(etName.getText().toString(), etAge.getText().toString(), url).start();            }        });    }

android http通訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.