HttpUrlConnection网络请求

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

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

public class WebViewActivity extends AppCompatActivity {
    WebView webView;
    Button httpBtn;
    TextView sucess;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        webView = (WebView) findViewById(R.id.webView);
        //webView.loadUrl("http://www.baidu.com");
        sucess = (TextView) findViewById(R.id.sucess);
        httpBtn = (Button) findViewById(R.id.button);
        httpBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        HttpUrlConnectionGet();
                    }
                }).start();
            }
        });
    }
    public void HttpUrlConnectionGet(){
        HttpURLConnection uRLConnection = null;
        InputStream is;
        StringBuilder sb = new StringBuilder();
        try {
            URL url = new URL("http://apis.baidu.com/txapi/naowan/naowan");
            uRLConnection = (HttpURLConnection) url.openConnection();
            uRLConnection.setConnectTimeout(5*1000);
            uRLConnection.setReadTimeout(5*1000);
            uRLConnection.setRequestProperty("apikey","7f52de784d67f3c53c1c1d6cfdb2e563");
            uRLConnection.connect();
            if (uRLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                is = uRLConnection.getInputStream();
                byte[] bytes = new byte[1024];
                int i = 0;
                while ((i = is.read(bytes)) != -1 ) {
                    sb.append(new String(bytes,0,i,"utf-8"));
                }
                is.close();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (uRLConnection != null) {
                uRLConnection.disconnect();
            }
        }
        Message message = handler.obtainMessage(1,sb.toString());
        handler.sendMessage(message);
    }
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg != null && msg.what == 1) {
                String str = (String) msg.obj;
                webView.getSettings().setDefaultTextEncodingName("utf-8");
                webView.getSettings().setJavaScriptEnabled(true);
                webView.loadDataWithBaseURL(null,str,"txet/html","utf-8",null);
                sucess.setText("请求成功");
            }
        }
    };
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值