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("請求成功");
            }
        }
    };
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章