Android&String.index()

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         String count = getMidString(xml, "<count>", "</count>");
 36         String encode, decode, lrcid, musicUrlStr, lrcUrlStr;
 37         if(count != "0")
 38         { 
 39             encode = getMidString(xml, "<encode><![CDATA[", "]]></encode>");
 40             decode = getMidString(xml, "<decode><![CDATA[", "]]></decode>");
 41             lrcid = getMidString(xml, "<lrcid>", "</lrcid>");
 42             musicUrlStr = getMusicUrlStr(encode, decode);
 43             lrcUrlStr = getLrcUrlStr(lrcid);
 44             myTextView.setText(musicUrlStr + "\n******\n" + lrcUrlStr);
 45         }
 46         else
 47             myTextView.setText("Not found");
 48     }
 49     public String getMidString(String inputstring, String left,String right){
 50         int k0 = inputstring.indexOf(left);
 51         if(k0<0){
 52         return "";
 53         }
 54         int k1= k0+left.length();
 55         int k2 = inputstring.indexOf(right,k1);
 56         if(k2<=k1){
 57         return "";
 58         }
 59         String return_v = inputstring.substring(k1,k2);
 60         return return_v;
 61         }
 62     
 63     public String getMusicUrlStr(String encode, String decode){
 64         int k0 = encode.lastIndexOf("/");
 65         if(k0<0)
 66             return "";
 67         String musicUrlStr = encode.substring(0, k0+1) + decode;
 68         return musicUrlStr;
 69     }
 70     
 71     public String getLrcUrlStr(String lrcid){
 72         if(lrcid == "0")
 73             return "";
 74         String lrcUrlStr = "http://box.zhangmen.baidu.com/bdlrc/";
 75         String mid = lrcid.substring(0, lrcid.length()-2);
 76         return lrcUrlStr + mid + "/" + lrcid + ".lrc";
 77     }
 78     class downloadThread extends Thread {
 79         String urlStr;
 80         downloadThread(String urlStr){
 81             this.urlStr = urlStr;
 82             Log.i(TAG, "Creat the Thread");
 83         }
 84 
 85         @Override
 86         public void run() {
 87             StringBuffer sb = new StringBuffer();
 88             String line = null;
 89             BufferedReader buffer = null;
 90             try {
 91                 URL url = new URL(urlStr);
 92                 HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
 93                 buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
 94                 while((line = buffer.readLine())!=null)
 95                     sb.append(line);
 96             } catch (MalformedURLException e) {
 97                 e.printStackTrace();
 98             } catch (IOException e) {
 99                 e.printStackTrace();
100             }finally{
101                 try {
102                     buffer.close();
103                     xml = sb.toString();
104                     finish = true;
105                     //myTextView.setText(xml);
106                     System.out.println(xml);
107                     Log.i(TAG, xml);
108                 } catch (IOException e) {
109                     e.printStackTrace();
110                 }
111             }
112             
113         }
114         
115         
116     }
117     
118     @Override
119     public boolean onCreateOptionsMenu(Menu menu) {
120         // Inflate the menu; this adds items to the action bar if it is present.
121         getMenuInflater().inflate(R.menu.main, menu);
122         return true;
123     }
124 
125 }

 

posted on 2013-04-19 20:19  Zachary_wiz  阅读(181)  评论(0编辑  收藏  举报

导航