具体的界面如下图所示

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>
View Code

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=readInstream(iStream);//得到数据13             14             //将字节转化为字符串15             return new String(data,enconding);16         }
View Code

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 }
View Code

5、修改AndroidManifest.xml代码,在</application>标签后加上如下代码:
<uses-permission android:name="android.permission.INTERNET"/>

更多相关文章

  1. 使用Android中的Parcelable序列化对象
  2. android闹钟――原代码
  3. android XMl 解析神奇xstream 二: 把对象转换成xml
  4. android编写访问http的代码
  5. Android实现TextView部分文本监听单击事件
  6. 跟着第二行代码回顾Android--持久化技术
  7. Android常用三栏式滑动/滚动视图(View)的设计与实现代码
  8. Android Http请求的代码

随机推荐

  1. 【OpenCV4Android学习一】 Android(安卓)
  2. Android 更改软键盘Enter键为搜索
  3. Android集成Huawei PUSH(四)——客户端开发
  4. android 分辨率问题
  5. Android通过JNI调用驱动程序(完全解析实
  6. Android应用程序添加自定义的property属
  7. Android(1.初识Android应用程序目录)
  8. android 出现gen already exists but is
  9. Android中定时器Timer和TimerTask的启动,
  10. 第一章 开始启程,你的第一行Android代码