80,351
社区成员
发帖
与我相关
我的任务
分享
<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>
<?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>
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();
}
}
}
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();
}
}
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);
}
}
}
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;
}
}