zoukankan      html  css  js  c++  java
  • HttpURLConnection

    package com.pingyijinren.test;
    
    import android.content.Intent;
    import android.os.Handler;
    import android.os.Message;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class MainActivity extends AppCompatActivity{
        private Button button;
        private TextView textView;
        private static final int SHOW_RESPONSE=0;
        private Handler handler=new Handler(){
            public void handleMessage(Message message){
                switch(message.what){
                    case SHOW_RESPONSE:
                        String response=(String)message.obj;
                        textView.setText(response);
                }
            }
        };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            button=(Button)findViewById(R.id.button);
            textView=(TextView)findViewById(R.id.textView);
    
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sendRequestWithHttpURLConnection();
                }
            });
        }
    
        private void sendRequestWithHttpURLConnection(){
            new Thread(new Runnable(){
                @Override
                public void run(){
                    HttpURLConnection httpURLConnection=null;
    
                    try{
                        URL url=new URL("https://www.baidu.com");
                        httpURLConnection=(HttpURLConnection)url.openConnection();
                        httpURLConnection.setRequestMethod("GET");
                        httpURLConnection.setConnectTimeout(8000);
                        httpURLConnection.setReadTimeout(8000);
                        InputStream in=httpURLConnection.getInputStream();
                        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(in));
                        StringBuilder response=new StringBuilder();
                        String line;
                        while((line= bufferedReader.readLine())!=null){
                            response.append(line);
                        }
    
                        Message message=new Message();
                        message.what=SHOW_RESPONSE;
                        message.obj=response.toString();
                        handler.sendMessage(message);
                    }
                    catch(Exception e){
                        e.printStackTrace();
                    }
                    finally {
                        if(httpURLConnection!=null){
                            httpURLConnection.disconnect();
                        }
                    }
                }
            }).start();
        }
    }
  • 相关阅读:
    使用FreeTextBox等控件带来的问题
    DoNet分页控件
    享元模式学习后总结!(见到别人的总结,解我心中迷惑,认可)
    基于工作流平台的ITSM系统
    .net工作流在移动公司的部署
    转载:When Office 2003 couldn’t find file SKU011.CAB Office 2003 reinstallation error
    联通公司代理商佣金管理
    知识管理系统分析之一:网络蜘蛛的分析
    自我介绍像猪一样生活
    知识管理的整体架构
  • 原文地址:https://www.cnblogs.com/zqxLonely/p/5507694.html
Copyright © 2011-2022 走看看