我是一个新手,目前在跟着教材写程序这个阶段。最近在学http网络编程。在这样一个小程序上栽倒了。 为了测试我把程序简化了。布局文件只有一个TextView。创建了一个用于获取某个网址返回的字符串的工具类。主要是通过Connection的getInputStream方法获取输入流,再通过BufferedReader读取到一个字符串。然后在一个继承了AsyncTask的内部类的doInBackground方法里调用该工具类的相关方法返回一个字符串,在onPostExecute方法里通过TextView的setText方法显示该字符串。在onCreate方法中调用该内部类的execute方法。写完代码,发布apk文件。安装到手机上,界面一片空白。没有显示应有的信息。因为我的eclipse的模拟器有问题奇慢无比。我用网易的mumu手机模拟器运行发现程序是可以显示出正确的信息的。再次安装到手机上依旧一片空白。不知哪位大神遇到过类似的问题。还请不吝赐教,把这个绝望的人从坑里拉出来吧。
activity_thread_demo.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
thread_demo_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="test"/>
<Button android:id="@+id/bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="test"/>
</LinearLayout>
SingleFragmentActivity.java这个类用于获取FragmentManager并调用相应的Fragment
package com.lili.android.threaddemo;import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.widget.ListView;public abstract class SingleFragmentActivity extends FragmentActivity {
protected abstract Fragment createFragment();
protected int getLayoutRes(){
return R.layout.activity_thread_demo;
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(getLayoutRes());
FragmentManager fm=getSupportFragmentManager();
Fragment ft=fm.findFragmentById(R.id.fragmentContainer);
if(ft==null){
ft=createFragment();
fm.beginTransaction().add(R.id.fragmentContainer, ft).commit();
}
}
}ThreadDemoActivity.java这个是被托管的Activity
package com.lili.android.threaddemo;import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Menu;
import android.view.MenuItem;
public class ThreadDemoActivity extends SingleFragmentActivity { protected Fragment createFragment(){
return new ThreadDemoFragment();
}
}ThreadDemoFragment.java 这个类是托管activity的fragment类,程序的主要功能都是这个类实现的.
package com.lili.android.threaddemo;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;public class ThreadDemoFragment extends Fragment {
TextView textView;
public void onCreate(Bundle arg0){
super.onCreate(arg0);
new ThreadTest1().execute();
}
public View onCreateView(LayoutInflater inflater,ViewGroup parent,Bundle b){
View v=inflater.inflate(R.layout.thread_demo_fragment, parent, false);
textView=v.findViewById(R.id.textView);
return v;
}
private class ThreadTest1 extends AsyncTask<Void,Void,String>{
public String doInBackground(Void... params){
String tmp="initial";
try{
tmp=new NetInfoFetcher().getURL("http://www.baidu.com");
}catch(IOException e){
}
return tmp;
}
public void onPostExecute(String arg0){
textView.setText(arg0);
}
}
}NetInfoFetcher.java这个类是工具类,实现获取某个网址的返回字符串package com.lili.android.threaddemo;import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;public class NetInfoFetcher {
public String test(){
return "这是测试方法";
}
public String getURL(String url)throws IOException{
URL tmpURL=new URL(url);
HttpURLConnection conn=(HttpURLConnection)tmpURL.openConnection();
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(true);
conn.connect();
String resultData="";
try{
//ByteArrayOutputStream out=new ByteArrayOutputStream();
InputStream in=conn.getInputStream();
InputStreamReader inReader=new InputStreamReader(in);
BufferedReader buffReader=new BufferedReader(inReader);
String result="";
while((result=buffReader.readLine())!=null){
resultData=resultData+result+"\n";
}
}catch(IOException e){
resultData="erro";}
finally{
conn.disconnect();
}
return resultData;
}
}权限我是在AndroidManifest.xml文件中加入
<uses-permission android:name="android.permission.INTERNET"/>
这是电脑上的手机模拟器运行效果这是手机运行效果按钮是没用的,我本想加入别的功能,结果这个测试不过去。也就没加。