qq_29592587 2015-07-26 08:29 采纳率: 0%
浏览 1752
已结题

关于urlconnection的问题

求助!我写了一个程序去读取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;
        }
    }
};

}

  • 写回答

1条回答 默认 最新

  • danielinbiti 2015-07-26 08:40
    关注

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

    评论

报告相同问题?

悬赏问题

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