码迷,mamicode.com
首页 > 移动开发 > 详细

android http通信

时间:2015-06-15 09:19:02      阅读:174      评论:0      收藏:0      [点我收藏+]

标签: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通信

标签:android   http   

原文地址:http://blog.csdn.net/a4384142/article/details/46495635

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!