«

Android中使用HttpURLConnection和HttpClient实现GET和POST请求访问网络

时间:2024-3-2 18:27     作者:韩俊     分类: Android


HttpURLConnection中的GET

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Base64;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private EditText content;   //声明一个输入文本内容的编辑框对象
    private Button button;  //声明一个发表按钮对象
    private Handler handler; // 声明一个Handler对象
    private String result = ""; //声明一个代表显示内容的字符串
    private TextView resultTV;  //声明一个显示结果的文本框对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        content = (EditText) findViewById(R.id.content);    //获取输入文本内容的EditText组件
        resultTV = (TextView) findViewById(R.id.result);    //获取显示结果的TextView组件
        button = (Button) findViewById(R.id.button);    //获取“发表”按钮组件
        //为按钮添加单击事件监听器
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("".equals(content.getText().toString())) {
                    Toast.makeText(MainActivity.this, "请输入要发表的内容!",
                            Toast.LENGTH_SHORT).show(); //显示消息提示
                    return;
                }

                // 创建一个新线程,用于发送并读取微博信息
                new Thread(new Runnable() {
                    public void run() {
                        send(); //发送文本内容到Web服务器
                        Message m = handler.obtainMessage(); // 获取一个Message
                        handler.sendMessage(m); // 发送消息
                    }
                }).start(); // 开启线程
            }
        });
        //创建一个Handler对象
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (result != null) {
                    resultTV.setText(result); // 显示获得的结果
                    content.setText("");        //清空文本框
                }
                super.handleMessage(msg);
            }
        };
    }

    public void send() {
        String target="";
        target = "http://192.168.1.66:8081/blog/index.jsp?content="
                    +base64(content.getText().toString().trim());   //要访问的URL地址
        URL url;
        try {
            url = new URL(target);
            HttpURLConnection urlConn = (HttpURLConnection) url
                    .openConnection();  //创建一个HTTP连接
            InputStreamReader in = new InputStreamReader(
                    urlConn.getInputStream()); // 获得读取的内容
            BufferedReader buffer = new BufferedReader(in); // 获取输入流对象
            String inputLine = null;
            //通过循环逐行读取输入流中的内容
            while ((inputLine = buffer.readLine()) != null) {
                result += inputLine + "n";
            }
            in.close(); //关闭字符输入流对象
            urlConn.disconnect();   //断开连接
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //对字符串进行Base64编码
    public String base64(String content){
        try {
            content=Base64.encodeToString(content.getBytes("utf-8"), Base64.DEFAULT);   //对字符串进行Base64编码
            content=URLEncoder.encode(content); //对字符串进行URL编码
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();    //输出异常信息
        }
        return content;
    }
}

HttpURLConnection中的POST

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private EditText nickname; // 声明一个输入昵称的编辑框对象
    private EditText content; // 声明一个输入文本内容的编辑框对象
    private Button button; // 声明一个发表按钮对象
    private Handler handler; // 声明一个Handler对象
    private String result = ""; // 声明一个代表显示内容的字符串
    private TextView resultTV; // 声明一个显示结果的文本框对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        content = (EditText) findViewById(R.id.content); // 获取输入文本内容的EditText组件
        resultTV = (TextView) findViewById(R.id.result); // 获取显示结果的TextView组件
        nickname = (EditText) findViewById(R.id.nickname); // 获取输入昵称的EditText组件
        button = (Button) findViewById(R.id.button); // 获取“发表”按钮组件
        // 为按钮添加单击事件监听器
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("".equals(nickname.getText().toString())
                        || "".equals(content.getText().toString())) {
                    Toast.makeText(MainActivity.this, "请将内容输入完整!",
                            Toast.LENGTH_SHORT).show();
                    return;
                }

                // 创建一个新线程,用于从网络上获取文件
                new Thread(new Runnable() {
                    public void run() {
                        send();
                        Message m = handler.obtainMessage(); // 获取一个Message
                        handler.sendMessage(m); // 发送消息
                    }
                }).start(); // 开启线程

            }
        });
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (result != null) {
                    resultTV.setText(result); // 显示获得的结果
                    content.setText(""); // 清空内容编辑框
                    nickname.setText(""); // 清空昵称编辑框
                }
                super.handleMessage(msg);
            }
        };
    }

    public void send() {
        String target = "http://192.168.1.66:8081/blog/dealPost.jsp";   //要提交的目标地址
        URL url;
        try {
            url = new URL(target);
            HttpURLConnection urlConn = (HttpURLConnection) url
                    .openConnection(); // 创建一个HTTP连接
            urlConn.setRequestMethod("POST"); // 指定使用POST请求方式
            urlConn.setDoInput(true); // 向连接中写入数据
            urlConn.setDoOutput(true); // 从连接中读取数据
            urlConn.setUseCaches(false); // 禁止缓存
            urlConn.setInstanceFollowRedirects(true);   //自动执行HTTP重定向
            urlConn.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded"); // 设置内容类型
            DataOutputStream out = new DataOutputStream(
                    urlConn.getOutputStream()); // 获取输出流
            String param = "nickname="
                    + URLEncoder.encode(nickname.getText().toString(), "utf-8")
                    + "&content="
                    + URLEncoder.encode(content.getText().toString(), "utf-8"); //连接要提交的数据
            out.writeBytes(param);//将要传递的数据写入数据输出流
            out.flush();    //输出缓存
            out.close();    //关闭数据输出流
            // 判断是否响应成功
            if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                InputStreamReader in = new InputStreamReader(
                        urlConn.getInputStream()); // 获得读取的内容
                BufferedReader buffer = new BufferedReader(in); // 获取输入流对象
                String inputLine = null;
                while ((inputLine = buffer.readLine()) != null) {
                    result += inputLine + "n";
                }
                in.close(); //关闭字符输入流
            }
            urlConn.disconnect();   //断开连接
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

HttpClient的GET

和HttpURLConnection中的Get上述代码中唯一不同的是send方法;

public void send() {
        String target = "http://192.168.1.66:8081/blog/deal_httpclient.jsp?param=get";  //要提交的目标地址
        HttpClient httpclient = new DefaultHttpClient();//创建HttpClient对象
        HttpGet httpRequest = new HttpGet(target);  //创建HttpGet连接对象
        HttpResponse httpResponse;
        try {
            httpResponse = httpclient.execute(httpRequest); //执行HttpClient请求
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                result = EntityUtils.toString(httpResponse.getEntity());    //获取返回的字符串
            }else{
                result="请求失败!";
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

HttpClient的POST

同样的和HttpURLConnection中的Post不同的是send方法:

public void send() {
        String target = "http://192.168.1.66:8081/blog/deal_httpclient.jsp";    //要提交的目标地址
        HttpClient httpclient = new DefaultHttpClient();    //创建HttpClient对象
        HttpPost httpRequest = new HttpPost(target);    //创建HttpPost对象
        //将要传递的参数保存到List集合中
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("param", "post"));    //标记参数
        params.add(new BasicNameValuePair("nickname", nickname.getText().toString()));  //昵称
        params.add(new BasicNameValuePair("content", content.getText().toString()));    //内容
        try {
            httpRequest.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //设置编码方式
            HttpResponse httpResponse = httpclient.execute(httpRequest);    //执行HttpClient请求
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){  //如果请求成功
                result += EntityUtils.toString(httpResponse.getEntity());   //获取返回的字符串

            }else{
                result = "请求失败!";
            }
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();   //输出异常信息
        } catch (ClientProtocolException e) {
            e.printStackTrace();    //输出异常信息
        } catch (IOException e) {
            e.printStackTrace();    //输出异常信息
        }
    }

标签: android

热门推荐