zoukankan      html  css  js  c++  java
  • Android&下载文件xml

    MainActivity.java
     1 package com.zachary.xmlresolve;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.IOException;
     5 import java.io.InputStreamReader;
     6 import java.net.HttpURLConnection;
     7 import java.net.MalformedURLException;
     8 import java.net.URL;
     9 
    10 import android.app.Activity;
    11 import android.os.Bundle;
    12 import android.util.Log;
    13 import android.view.Menu;
    14 import android.widget.TextView;
    15 
    16 public class MainActivity extends Activity {
    17     private TextView myTextView;
    18     private String xml;
    19     private boolean finish = false;
    20     final static String TAG = "LOGCAT";
    21     @Override
    22     protected void onCreate(Bundle savedInstanceState) {
    23         super.onCreate(savedInstanceState);
    24         setContentView(R.layout.activity_main);
    25         //String xmlStr = "![CDATA[你好啊 。。。   ]]";
    26         myTextView = (TextView)findViewById(R.id.myTextView);
    27         //xml = getMidString(xmlStr, "![CDATA[", "]]");
    28         //myTextView.setText(xml);
    29         //System.out.println(xml);
    30         String urlStr ="http://box.zhangmen.baidu.com/x?op=12&count=1&title=%E6%80%92%E6%94%BE%E7%9A%84%E7%94%9F%E5%91%BD$$%E6%B1%AA%E5%B3%B0$$$$";
    31         new downloadThread(urlStr).start();
    32         while(!finish) ;
    33         myTextView.setText(xml);
    34         finish = false;
    35     }
    36     
    37     public String getMidString(String inputstring, String left,String right){
    38         int k0 = inputstring.indexOf(left);
    39         if(k0<0){
    40         return "";
    41         }
    42         int k1= k0+left.length();
    43         int k2 = inputstring.indexOf(right,k1);
    44         if(k2<=k1){
    45         return "";
    46         }
    47         String return_v = inputstring.substring(k1,k2);
    48         return return_v;
    49         }
    50     
    51     class downloadThread extends Thread {
    52         String urlStr;
    53         downloadThread(String urlStr){
    54             this.urlStr = urlStr;
    55             Log.i(TAG, "Creat the Thread");
    56         }
    57 
    58         @Override
    59         public void run() {
    60             StringBuffer sb = new StringBuffer();
    61             String line = null;
    62             BufferedReader buffer = null;
    63             try {
    64                 URL url = new URL(urlStr);
    65                 HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
    66                 buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
    67                 while((line = buffer.readLine())!=null)
    68                     sb.append(line);
    69             } catch (MalformedURLException e) {
    70                 e.printStackTrace();
    71             } catch (IOException e) {
    72                 e.printStackTrace();
    73             }finally{
    74                 try {
    75                     buffer.close();
    76                     xml = sb.toString();
    77                     finish = true;
    78                     //myTextView.setText(xml);
    79                     System.out.println(xml);
    80                     Log.i(TAG, xml);
    81                 } catch (IOException e) {
    82                     e.printStackTrace();
    83                 }
    84             }
    85             
    86         }
    87         
    88         
    89     }
    90     
    91     @Override
    92     public boolean onCreateOptionsMenu(Menu menu) {
    93         // Inflate the menu; this adds items to the action bar if it is present.
    94         getMenuInflater().inflate(R.menu.main, menu);
    95         return true;
    96     }
    97 
    98 }
  • 相关阅读:
    java生成UUID通用唯一识别码 (Universally Unique Identifier)
    使用ToolRunner运行Hadoop程序基本原理分析
    Hadoop入门经典:WordCount
    Hadoop配置文件
    【Nutch2.2.1基础教程之3】Nutch2.2.1配置文件
    8大排序算法图文讲解
    动态字典树_前缀相互查找(HDU_1671)
    DP_基本DP+排序(HDU_1421)
    斯特灵公式
    七种 qsort 排序方法
  • 原文地址:https://www.cnblogs.com/wizzhangquan/p/3030432.html
Copyright © 2011-2022 走看看