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有没有用错

    评论

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常