首页 > 代码库 > Android下载网络文本
Android下载网络文本
具体的界面如下图所示
从浏览器上下载文本文件到手机中,具体的步骤如下:
1、新建一个工程名为GetTxtData的项目;
2、修改res/layout/fragment_main.xml文件,具体代码如下:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.gettxtdata.MainActivity$PlaceholderFragment" >10 11 <LinearLayout 12 android:layout_width="fill_parent"13 android:layout_height="fill_parent"14 android:orientation="vertical"15 >16 <EditText 17 android:id="@+id/inputText"18 android:layout_width="fill_parent"19 android:layout_height="wrap_content"20 android:text="http://www.sohu.com"21 />22 <Button 23 android:layout_width="wrap_content"24 android:layout_height="wrap_content"25 android:id="@+id/getTxtBtn"26 android:text="获取文本"27 />28 <ScrollView 29 android:layout_width="fill_parent"30 android:layout_height="fill_parent"31 >32 <TextView android:layout_width="fill_parent"33 android:layout_height="wrap_content"34 android:id="@+id/sohu"35 />36 </ScrollView>37 </LinearLayout>38 39 40 </RelativeLayout>
3、新建一个名为NetTool.java的类,该类主要是操作文本的,类中添加的方法如下所示:
1 public static String getHtml(String path,String enconding)throws Exception{ 2 //建立url类对象,抛出异常 3 URL url=new URL(path); 4 //得到HttpURLConnection对象 5 HttpURLConnection conn=(HttpURLConnection)url.openConnection(); 6 conn.setRequestMethod("GET");//声明请求方式 7 conn.setConnectTimeout(6*1000);//设置链接超时 8 9 if(conn.getResponseCode()==200)10 {11 InputStream iStream=conn.getInputStream();//得到输入流12 byte[] data=http://www.mamicode.com/readInstream(iStream);//得到数据13 14 //将字节转化为字符串15 return new String(data,enconding);16 }
4、修改MainActivity.java代码,代码如下:
1 public class MainActivity extends ActionBarActivity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 8 if (savedInstanceState == null) { 9 getSupportFragmentManager().beginTransaction()10 .add(R.id.container, new PlaceholderFragment()).commit();11 }12 }13 14 @Override15 public boolean onCreateOptionsMenu(Menu menu) {16 17 // Inflate the menu; this adds items to the action bar if it is present.18 getMenuInflater().inflate(R.menu.main, menu);19 return true;20 }21 22 @Override23 public boolean onOptionsItemSelected(MenuItem item) {24 // Handle action bar item clicks here. The action bar will25 // automatically handle clicks on the Home/Up button, so long26 // as you specify a parent activity in AndroidManifest.xml.27 int id = item.getItemId();28 if (id == R.id.action_settings) {29 return true;30 }31 return super.onOptionsItemSelected(item);32 }33 34 /**35 * A placeholder fragment containing a simple view.36 */37 @TargetApi(Build.VERSION_CODES.GINGERBREAD)38 public class PlaceholderFragment extends Fragment {39 40 public PlaceholderFragment() {41 }42 43 @Override44 public View onCreateView(LayoutInflater inflater, ViewGroup container,45 Bundle savedInstanceState) {46 View rootView = inflater.inflate(R.layout.fragment_main, container,47 false);48 StrictMode.setThreadPolicy(new 49 StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());50 StrictMode.setVmPolicy(51 new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());52 53 54 final EditText inputText=(EditText)rootView.findViewById(R.id.inputText);55 final Button getBtn=(Button)rootView.findViewById(R.id.getTxtBtn);56 final TextView txtView=(TextView)rootView.findViewById(R.id.sohu);57 58 getBtn.setOnClickListener(new View.OnClickListener() {59 60 @Override61 public void onClick(View v) {62 // TODO Auto-generated method stub63 String path=inputText.getText().toString();64 try65 {66 String html=NetTool.getHtml(path, "GBK");67 txtView.setText(html);68 69 Toast.makeText(MainActivity.this, 70 "获取文本成功", Toast.LENGTH_LONG).show();71 }catch(Exception e)72 {73 Toast.makeText(MainActivity.this, 74 "获取文本失败", Toast.LENGTH_LONG).show();75 e.printStackTrace();76 }77 }78 });79 return rootView;80 }81 }82 83 }
5、修改AndroidManifest.xml代码,在</application>标签后加上如下代码:
<uses-permission android:name="android.permission.INTERNET"/>
Android下载网络文本
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。