Android下载网络文本_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > Android下载网络文本

Android下载网络文本

 2014/12/26 23:22:51  帝权  程序员俱乐部  我要评论(0)
  • 摘要:具体的界面如下图所示从浏览器上下载文本文件到手机中,具体的步骤如下:1、新建一个工程名为GetTxtData的项目;2、修改res/layout/fragment_main.xml文件,具体代码如下:1<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"2xmlns:tools="http://schemas.android.com/tools"3android
  • 标签:android 下载 网络

具体的界面如下图所示

从浏览器上下载文本文件到手机中,具体的步骤如下:

1、新建一个工程名为GetTxtData的项目;

2、修改res/layout/fragment_main.xml文件,具体代码如下:

class="code_img_closed" src="/Upload/Images/2014122623/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('14fd3365-a932-472c-a8aa-d6fa86c93b7b',event)" src="/Upload/Images/2014122623/2B1B950FA3DF188F.gif" alt="" />
 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     @Override
15     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     @Override
23     public boolean onOptionsItemSelected(MenuItem item) {
24         // Handle action bar item clicks here. The action bar will
25         // automatically handle clicks on the Home/Up button, so long
26         // 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         @Override
44         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                 @Override
61                 public void onClick(View v) {
62                     // TODO Auto-generated method stub
63                     String path=inputText.getText().toString();
64                     try
65                     {
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"/>

 

发表评论
用户名: 匿名