当前位置:编程学习 > wap >>

新手--关于android主程序不能之间访问网络的问题

写了一个读取网络文件的简单程序,一个MainActivity.java主程序,一个HttpDonwLoader类,,负责下载和读取文件,,里面有一个donwload方法返回读取文件的字符串。。我知道android新的sdk不允许主程序直接联网,所以在主程序中新开了一个Thread来读取文件并返回String,但是在download方法中用HttpURLConnect.getInputStrean()获得的对象都是null,,求指教,,并说说我打印出来的那两个线程是属于一个线程吗?



MainAvtivity.java

package com.example.downloader;

import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

private Button ReadText = null;
String str = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println(Thread.currentThread());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ReadText = (Button) findViewById(R.id.ReadText);
ReadText.setOnClickListener(new ReadTextListener());

}

class ReadTextListener implements OnClickListener {

@Override
public void onClick(View v) {

new Thread(run).start();

}

}

Runnable run = new Runnable() {

@Override
public void run() {
System.out.println(Thread.currentThread());
HttpDonwLoader httpDonwLoader = new HttpDonwLoader();

str = httpDonwLoader
.donwload("http://192.168.56.1:8080/TCA/test.txt");
System.out.println("donw after");
System.out.println(str);

}

};

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

}




HttpDonwLoader.java



package com.example.downloader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.Buffer;

public class HttpDonwLoader {

private URL url = null;
String urlStr = "";
String result = "";
public String donwload (String urlStr) {
this.urlStr = urlStr;
Thread t = new Thread(r);
t.start();
return result;
}

Runnable r = new Runnable() {

@Override
public void run() {


StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader bf = null;

try {
url = new URL(HttpDonwLoader.this.urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (conn != null) {

System.out.println(conn);
}
// System.out.println(Thread.currentThread());

bf = new BufferedReader(new InputStreamReader(conn.getInputStream()));
System.out.println("bf");

while ((line = bf.readLine()) != null) {
System.out.println(line);
sb.append(line);
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {

try {
if (bf != null){

bf.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

HttpDonwLoader.this.result = sb.toString();





}


};

}




--------------------编程问答-------------------- 首先,两个线程肯定不是同一个,从名字可以就可以看出来。
其次,没有看出你的代码中关于openConnection和getInputStream调用中的问题。
最后,你已经在onClick函数里创建了一个新的线程,所以就没有必要在download函数中另外创建线程。在download中创建线程带来另外一个副作用就是它很可能无法返回你想要的结果。因为它返回的时候,HttpDonwLoader.this.result = sb.toString();很可能还没有执行到。 --------------------编程问答--------------------
引用 1 楼 Walkerr 的回复:
首先,两个线程肯定不是同一个,从名字可以就可以看出来。
其次,没有看出你的代码中关于openConnection和getInputStream调用中的问题。
最后,你已经在onClick函数里创建了一个新的线程,所以就没有必要在download函数中另外创建线程。在download中创建线程带来另外一个副作用就是它很可能无法返回你想要的结果。因为它返回的时候,HttpDonwLoader.this.result = sb.toString();很可能还没有执行到。


我把download中的那个线程去掉了,,还是不可以,,你说getInputStream没有问题,,但是它下面的那个bf输出还是输不出来.
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,