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 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)