2 qq 29592587 qq_29592587 于 2015.07.26 16:29 提问

关于urlconnection的问题 20C

求助!我写了一个程序去读取IIS服务器上一个txt里的内容 代码如下:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
private TextView myTextView;
public String copy;
private Thread th1;
private String str;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTextView=(TextView)findViewById(R.id.T1);
th1=new Thread(new mythr());
th1.start();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

class mythr implements Runnable
{
    public void run() {
        while (true) {
            try {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                URL url;
                url = new URL("http://218.199.151.96/data.txt");
                URLConnection hpCon = url.openConnection();//用地址的url创建连接
                int len = hpCon.getContentLength();
                hpCon.connect();
                InputStream input = hpCon.getInputStream();
                str = inputStream2String(input);
                Message msgg = new Message();
                msgg.what = 100;
                mHandler.sendMessage(msgg);//输出页面的文本内容,为html代码;
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}


public static String inputStream2String(InputStream is) throws IOException    //输入流转换为string
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int i = -1;
    while ((i = is.read()) != -1)
    {
        baos.write(i);
    }
    return baos.toString();
}

private Handler mHandler = new Handler() {
    public void handleMessage(Message msg) {
        switch (msg.what){
            case 100:
                myTextView.setText(str);
                break;
        }
    }
};

}
这都能够运行 可是把这段代码中的线程以及方法插入到另一个activity里测试为什么始终是没有反应?
另一个activity如下:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class first extends Activity {
Intent intent = new Intent();
private TextView myTextView;
public String copy;
private Thread th1;
private String str;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
intent.setClass(first.this, com.kince.waveview.MainActivity.class);
myTextView=(TextView)findViewById(R.id.T1);
th1=new Thread(new mythr());
th1.start();
Button b1 = (Button)findViewById(R.id.button1);

    b1.setOnClickListener(new Button.OnClickListener()
    {
        public void onClick(View v)
        {
            startActivity(intent);
        }
    });
}

class mythr implements Runnable
{
    public void run() {
        while (true) {
            try {

                URL url;
                url = new URL("http://218.199.151.96/data.txt");
                URLConnection hpCon = url.openConnection();//用地址的url创建连接
                int len = hpCon.getContentLength();
                hpCon.connect();
                System.out.println("Content-leng:" + len);
                System.out.println("--------------content-------------");
                InputStream input = hpCon.getInputStream();
                str = inputStream2String(input);
                System.out.println(str);
                Message msgg = new Message();
                msgg.what = 100;
                mHandler.sendMessage(msgg);//输出页面的文本内容,为html代码;
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}


public static String inputStream2String(InputStream is) throws IOException    //输入流转换为string
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int i = -1;
    while ((i = is.read()) != -1)
    {
        baos.write(i);
    }
    return baos.toString();
}

private Handler mHandler = new Handler() {
    public void handleMessage(Message msg) {
        switch (msg.what){
            case 100:
                myTextView.setText(str);
                break;
        }
    }
};

}

2个回答

danielinbiti
danielinbiti   Ds   Rxr 2015.07.26 16:40

看看有没有报错,另外,看布局文件有没有错,textview的id有没有用错

danielinbiti
danielinbiti 回复qq_30137133: 先看日志有没有错误
2 年多之前 回复
qq_30137133
qq_30137133 难道没有网
2 年多之前 回复
qq_29592587
qq_29592587 没有报错 布局文件是在之前可以运行的一个程序的布局文件里加了linearlayout textview也是反复检查过了的
2 年多之前 回复
CSDNXIAON
CSDNXIAON   2015.07.26 16:40

URLConnection 中文参数乱码问题
URLConnection 使用流的问题
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

Csdn user default icon
上传中...
上传图片
插入图片