为了账号安全,请及时绑定邮箱和手机立即绑定

HttpUrlConnection中url写http不行,使用https可以

HttpUrlConnection中url写http不行,使用https可以

一条小咸鱼 2017-06-27 17:30:56
求解?使用http没有返回。访问的是http://www.baidu.com,使用GET方法。package com.example.sty.httpurlconnectiontest; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class MainActivity extends AppCompatActivity {     private static final int SHOW_RESPONSE = 0;     private Button mButton;     private Handler handler = new Handler() {         @Override         public void handleMessage(Message msg) {             switch (msg.what) {                 case SHOW_RESPONSE:                     mResponseText.setText((String) msg.obj);                     break;                 default:                     break;             }         }     };     private TextView mResponseText;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         mButton = (Button) findViewById(R.id.btn_send_request);         mResponseText = (TextView) findViewById(R.id.tv_response);         mButton.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View view) {                 sendRequestWithUrlConnection();             }         });     }     private void sendRequestWithUrlConnection() {         new Thread(new Runnable() {             @Override             public void run() {                 HttpURLConnection connection = null;                 try {                     URL url = new URL("http://www.baidu.com");                     connection = (HttpURLConnection) url.openConnection();                     connection.setRequestMethod("GET");                     connection.setConnectTimeout(5000);                     connection.setReadTimeout(5000);                     InputStream inputStream = connection.getInputStream();                     StringBuilder response = new StringBuilder();                     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));                     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 (IOException e) {                     e.printStackTrace();                 } finally {                     if (connection != null) {                         connection.disconnect();                     }                 }             }         }).start();     } }
查看完整描述

1 回答

?
蜂之谷

TA贡献564条经验 获得超863个赞

百度就是 https啊 不是http

查看完整回答
反对 回复 2017-06-27
  • 1 回答
  • 0 关注
  • 3205 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信