Hi我是天哥哥 2016-04-16 06:08 采纳率: 0%
浏览 1297

httpurlconnection的get问题 没有正常打开页面 内部按钮点击textview没反应

package com.example.http;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;

import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import android.widget.TextView;
import android.widget.Toast;

public class httpurlconnectionactivity extends Activity implements OnClickListener{
Button cleartextviewButton;
Button httpurlconnectiongetButton;
Button httpurlconnectionpostButton;
static TextView httpurlconnecTextView;

static Handler handler=new Handler(){
    public void handleMessage(Message msg) {
        switch(msg.what){
            case 5:
                String response=(String) msg.obj;
                httpurlconnecTextView.setText(response);

        }
    }
};
@Override

protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.httpurlconnecttion_layout);
cleartextviewButton=(Button) findViewById(R.id.cleartextview_button);
httpurlconnectiongetButton=(Button) findViewById(R.id.httpurlconnectionget_button);
httpurlconnectionpostButton=(Button) findViewById(R.id.httpurlconnectionpost_button);
cleartextviewButton.setOnClickListener(httpurlconnectionactivity.this);
httpurlconnectiongetButton.setOnClickListener(httpurlconnectionactivity.this);
httpurlconnectionpostButton.setOnClickListener(httpurlconnectionactivity.this);
httpurlconnecTextView=(TextView) findViewById(R.id.httpurlconnection_textview);

}

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.cleartextview_button:

        httpurlconnecTextView.setText("");
        break;
      case R.id.httpurlconnectionget_button:
          sendrequestwithhttpurlconnectionget();
          break;
          case R.id.httpurlconnectionpost_button:
              Toast toast=new Toast(httpurlconnectionactivity.this);

              Toast.makeText(httpurlconnectionactivity.this, "暂待实现", Toast.LENGTH_LONG);
              toast.show();

    default:
        break;
    }   
    }


private void sendrequestwithhttpurlconnectionget() {
new Thread(new Runnable() {

    @Override
    public void run() {
        HttpURLConnection httpURLConnection = null;
        // TODO Auto-generated method stub
        try {
            URL url=new URL("http://www.baidu.com");
            httpURLConnection=(HttpURLConnection) url.openConnection();
            httpURLConnection.setConnectTimeout(8000);
            httpURLConnection.setReadTimeout(8000);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestMethod("Get");
            //获取从httpurlconnection得来的输入流
            InputStream inputStream=httpURLConnection.getInputStream();
            //将输入流读入 读缓冲区
            BufferedReader bufferedReader=new BufferedReader
                    (new InputStreamReader(inputStream));
            //设置输入的行
            String line;
            //将写好的行加入response中
            StringBuilder response=new StringBuilder();
            while ((line=bufferedReader.readLine())!=null) {
                response.append(line);
            }
            Message message=new Message();
            message.what=5;
            message.obj=response.toString();
            handler.sendMessage(message);


        } catch (Exception e) {
            // TODO: handle exception
        }
        finally{
            if (httpURLConnection!=null) {
                httpURLConnection.disconnect();
            }
        }
    }
}).start();

}
}

  • 写回答

2条回答 默认 最新

  • Hi我是天哥哥 2016-04-16 06:09
    关注

    这是布局
    <?xml version="1.0" encoding="utf-8"?>
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="get操作"/>
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="post操作"/>

    <Button 
        android:id="@+id/cleartextview_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清空操作"/>
    <ScrollView android:layout_width="match_parent"
        android:layout_height="match_parent" >
    <TextView 
        android:id="@+id/httpurlconnection_textview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    


    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题