HttpURLConnection

请求头(get,post)
响应头(200 成功)–content-type(jpeg)mime—-content-length———服务端开发人员给我们的
响应正文—流文件

post
请求正文(提交给服务器的数据(流))
post请求 请求头多了两个数据content-type(jpeg)mime—-content-length,也是需要我们写给服务端开发人员

GET请求

package com.itheima.getmethod;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import com.itheima.htmlviewer.tools.Tools;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    //
    Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            Toast.makeText(MainActivity.this, (String)msg.obj, 0).show();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void click(View v){

        EditText et_name = (EditText) findViewById(R.id.et_name);
        EditText et_pass = (EditText) findViewById(R.id.et_pass);

        final String name = et_name.getText().toString();
        final String pass = et_pass.getText().toString();

        Thread t = new Thread(){
            @Override
            public void run() {
                @SuppressWarnings("deprecation")
                String path = "http://169.254.244.136/Web2/servlet/Login?name=" + URLEncoder.encode(name) + //对要提交的表单数据进行url编码
                                                                                        "&pass=" + pass;
                try {
                    URL url = new URL(path);
                    //打开connection连接
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    //设置请求方法
                    conn.setRequestMethod("GET");
                    //设置连接超时时间
                    conn.setConnectTimeout(8000);
                    //设置请求超时时间
                    conn.setReadTimeout(8000);

                    if(conn.getResponseCode() == 200){
                        InputStream is = conn.getInputStream();
                        String text = Tools.getTextFromStream(is);//设置方法读取流数据

                        Message msg = handler.obtainMessage();
                        msg.obj = text;
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        };
        t.start();
    }

}

Tool类方法

package com.itheima.htmlviewer.tools;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Tools {

    public static String getTextFromStream(InputStream is){
        byte[] b = new byte[1024];
        int len;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            while((len = is.read(b)) != -1){
                bos.write(b, 0, len);
            }
            //把字节数组输出流转换成字节数组,然后用字节数组构造一个字符串
            String text = new String(bos.toByteArray());
            return text;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;

    }
}
添加网络权限

POST请求
package com.itheima.postmethod;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import com.itheima.htmlviewer.tools.Tools;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {


    Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            Toast.makeText(MainActivity.this, (String)msg.obj, 0).show();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void click(View v){

        EditText et_name = (EditText) findViewById(R.id.et_name);
        EditText et_pass = (EditText) findViewById(R.id.et_pass);

        final String name = et_name.getText().toString();
        final String pass = et_pass.getText().toString();

        Thread t = new Thread(){
            @Override
            public void run() {
                String path = "http://169.254.244.136/Web2/servlet/Login";
                try {
                    URL url = new URL(path);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                    conn.setRequestMethod("POST");
                    conn.setConnectTimeout(8000);
                    conn.setReadTimeout(8000);

                    //添加post请求头中自动添加的属性
                    //流里的数据的mimetype
                    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    String content = "name=" + URLEncoder.encode(name) + "&pass=" + pass;
                    //流里数据的长度
                    conn.setRequestProperty("Content-Length", content.length() + "");

                    //打开连接对象的输出流
                    conn.setDoOutput(true);
                    //获取连接对象的输出流
                    OutputStream os = conn.getOutputStream();
                    //把数据写入输出流中
                    os.write(content.getBytes());

                    if(conn.getResponseCode() == 200){
                        InputStream is = conn.getInputStream();
                        String text = Tools.getTextFromStream(is);

                        Message msg = handler.obtainMessage();
                        msg.obj = text;
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        };
        t.start();
    }

}

“`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值