Android入门三 Http通信(实例)

android 采用的是服务端和客户端的形式进行通信.

服务端

通常是一个servlet.

package com.sjming.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class 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 {
        //this.doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        PrintWriter out = response.getWriter();
        out.println("name="+name+" age="+age);

        System.out.println("name="+name+" age="+age);
    }

}

客户端

RegisterActivity

package com.example.sujinming.qumiao;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class RegisterActivity extends Activity {

    private EditText name;
    private EditText age;
    private Button register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        name = (EditText) findViewById(R.id.name);
        age = (EditText)findViewById(R.id.age);
        register = (Button)findViewById(R.id.register);

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = "http://118.202.40.18:8080/Serve/MyServlet";
                new HttpThread(url, name.getText().toString(), age.getText().toString()).start();
            }
        });
    }
}

HttpThread

package com.example.sujinming.qumiao;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpThread extends Thread{

    String url;
    String name;
    String age;

    public HttpThread(String url, String name, String age){
        this.url = url;
        this.name = name;
        this.age = age;
    }

    private void doGet(){
        url = url+"?name="+name+"&age="+age;
        try {
            URL httpUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);

            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String str;
            StringBuffer sb = new StringBuffer();
            while((str = reader.readLine()) != null) {
                sb.append(str);
            }
            System.out.println("result:"+sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void doPost(){
        try {
            URL httpUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)httpUrl.openConnection();

            conn.setRequestMethod("POST");
            conn.setConnectTimeout(5000);
            OutputStream out = conn.getOutputStream();
            String content = "name="+name+"&age="+age;
            out.write(content.getBytes());
            //get the inf form serve
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String str;
            while((str = reader.readLine()) != null) {
                sb.append(str);
            }
            System.out.println("result:"+sb.toString());

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void run() {
        //doGet();
        doPost();
    }
}

你可能感兴趣的:(Android)