2013-10-12 57 views
0

我是從服務器通過使用校名作爲URL參數現在即時通訊讓我的應用程序脫機.suppose如果我從服務器獲取所有schoolname和文本文件保存在SD卡怎麼辦我歌廳學校資料搜尋值以學校名稱爲參數在本地serch schoolname?如何從文本文件

  URL2=www.xyz+ElementarySchools; 
      URL2=www.xyz+MiddleSchools; 
       URL2=www.xyz+HighSchools; 



      try { 
     Log.i("URL2",""+URL2); 

     HttpClient client = new DefaultHttpClient(); 
     HttpConnectionParams 
       .setConnectionTimeout(client.getParams(), 15000); 
     HttpConnectionParams.setSoTimeout(client.getParams(), 15000); 
     HttpUriRequest request = new HttpGet(URL2); 
     HttpResponse response = client.execute(request); 
     InputStream atomInputStream = response.getEntity().getContent(); 
     BufferedReader in = new BufferedReader(new  
        InputStreamReader(atomInputStream), 8192); 

     String line; 
     String str = ""; 
     while ((line = in.readLine()) != null) { 
      str += line; 
     } 



          JSONObject json2 = new JSONObject(str); 

     status = json2.getString("status"); 
     if (status.equals("1")) { 
      message = "data"; 

      JSONArray school = json2.getJSONArray("data"); 

      for (int i = 0; i < school.length(); i++) { 
       JSONObject object = school.getJSONObject(i); 

       Category_ID.add(Long.parseLong(object 
         .getString("school_id"))); 
       Category_name.add(object.getString("name")); 

      } 

     } 


           } 

    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     IOConnect = 1; 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 








       {"status":1,"data":[ 
      {"school_id":"321","name":"Chavez","phone":"","email":"", 
      "address":"","information":"","image":"","calendar_id":"2","id":"147","level_id":"1", 
      "title":"Elementary Schools"}, 

     {"school_id":"319","name":"Central","phone":"","email":"", 
    "address":"","information":"","image":"","calendar_id":"2", 
    "id":"145","level_id":"1","title":"Elementary Schools"}, 

     {"school_id":"318","name":"Carver","phone":"","email":"", 
    "address":"","information":"","image":"","calendar_id":"2", 
    "id":"144","level_id":"1","title":"Elementary Schools"}, 

    {"school_id":"317","name":"Carson","phone":"","email":"", 
    "address":"","information":"","image":"","calendar_id":"2","id":"143", 
    "level_id":"1","title":"Elementary Schools"}, 

{"school_id":"316","name":"Cadman","phone":"","email":"", 
"address":"","information":"","image":"","calendar_id":"1", 
"id":"142","level_id":"1","title":"Elementary Schools"}, 

{"school_id":"315","name":"Cabrillo","phone":"","email":"", 
"address":"","information":"","image":"","calendar_id":"1", 
"id":"141","level_id":"1","title":"Elementary Schools"}, 

{"school_id":"314","name":"Burbank","phone":"","email":"", 
"address":"","information":"","image":"","calendar_id":"2", 
"id":"140","level_id":"1","title":"Elementary Schools"}, 

{"school_id":"313","name":"Boone","phone":"","email":"", 
"address":"","information":"","image":"","calendar_id":"2", 
"id":"139","level_id":"1","title":"Elementary Schools"}, 

{"school_id":"498","name":"Zamorano","phone":"","email":"", 
"address":"","information":"","image":"","calendar_id":"2", 
"id":"324","level_id":"1","title":"Elementary Schools"}, 

{"school_id":"451","name":"Pershing","phone":"","email":"", 
"address":"","information":"","image":"","calendar_id":"1", 
"id":"277","level_id":"2","title":"Middle Schools"}, 

{"school_id":"454","name":"Preuss","phone":"","email":"", 
"address":"","information":"","image":"","calendar_id":"1", 
"id":"280","level_id":"2","title":"Middle Schools"}, 

{"school_id":"457","name":"Riley","phone":"","email":"", 
"address":"","information":"","image":"","calendar_id":"1", 
"id":"284","level_id":"2","title":"Middle Schools"}, 

{"school_id":"462","name":"Roosevelt","phone":"","email":"", 
"address":"","information":"","image":"","calendar_id":"1", 
"id":"288","level_id":"2","title":"Middle Schools"}, 

{"school_id":"468","name":"SCPA","phone":"","email":"", 
"address":"","information":"","image":"","calendar_id":"1", 
"id":"294","level_id":"2","title":"Middle Schools"}, 

{"school_id":"478","name":"Standley","phone":"","email":"", 
"address":"","information":"","image":"","calendar_id":"1", 
"id":"304","level_id":"2","title":"Middle Schools"}, 

{"school_id":"431","name":"Muir","phone":"","email":"", 
"address":"","information":"","image":"","calendar_id":"1", 
"id":"258","level_id":"3","title":"High Schools"}, 

{"school_id":"439","name":"O'Farrell","phone":"","email":"", 
"address":"","information":"","image":"","calendar_id":"1", 
"id":"267","level_id":"3","title":"High Schools"}, 


{"school_id":"452","name":"Point Loma","phone":"","email":"", 
"address":"","information":"","image":"","calendar_id":"1", 
"id":"278","level_id":"3","title":"High Schools"}, 

{"school_id":"454","name":"Preuss","phone":"","email":"", 
"address":"","information":"","image":"","calendar_id":"1", 
"id":"281","level_id":"3","title":"High Schools"}, 


{"school_id":"466","name":"San Diego","phone":"","email":"","address":"", 
"information":"","image":"","calendar_id":"1", 
"id":"292","level_id":"3","title":"High Schools"}]} 
+0

你要保存什麼? JSON響應? –

+3

如果你解析JSON響應,並將其存儲在數據庫中,並使用SQLite查詢來執行搜索 –

+0

@DharaShah你的建議就是完美這將是適當的...! –

回答

0

只需打開文本文件和閱讀所有文字,然後就看你是否解析JSON成對象或整個字符串運行的IndexOf。

下面介紹如何從文件中讀取文本,

public String readFile(File fFileIn) throws IOException{ 
    if(fFileIn == null) return ""; 
    if(!fFileIn.exists()) return ""; 

    FileInputStream fis = new FileInputStream(fFileIn); 
    byte [] bContent = new byte[(int) (fFileIn.length() + 1)]; 
    fis.read(bContent); 
    return new String(bContent, Charset.defaultCharset()); 
} 
+0

沒有我如何從文本文件獲得所有名稱是「中學」的學校? – user2867267

+0

見上面我改變什麼什麼東西來運行所有學校的文本文件的代碼獲取值名稱是「中學」 – user2867267

+0

哪種方法最快supose我的學校名單多於1000這樣歌廳應酌情學校在文本文件和serch含localy是我的代碼最快或者插入數據庫中的所有1000所學校,然後按名稱上學最快? – user2867267

0

當使用Web服務,建議,您維護本地和持久性存儲,脫機訪問。

通過本教程中去:http://androidituts.com/android-sqlite-database-tutorial/

你唯一需要改變的部分,而不是,:

Category_ID.add(Long.parseLong(object 
         .getString("school_id"))); 
Category_name.add(object.getString("name")); 

你只需要編寫數據庫插入查詢,這也是在提到以上教程。

希望我的建議適合您的需要...