问题描述
- 关于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; } } };
}
解决方案
看看有没有报错,另外,看布局文件有没有错,textview的id有没有用错
解决方案二:
URLConnection 中文参数乱码问题
URLConnection 使用流的问题