Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android http通信

android http通信

編輯:關於Android編程

***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();
            }
        });
    }
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved