2 hello gays Hello_gays 于 2016.04.16 14:08 提问

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个回答

Hello_gays
Hello_gays   2016.04.16 14: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"/>


Hello_gays
Hello_gays   2016.04.16 14:13

布局视图
图片说明

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!