0

I'm geting school information from server by using school name as parameter in URL now i m making my application offline .suppose if i get all schoolname from server and save in textfile in sd card how do i locally serch schoolname by give schoolname as parameter??

          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"}]}
Roberto
  • 11,557
  • 16
  • 54
  • 68
user2867267
  • 27
  • 3
  • 8

2 Answers2

0

Just open the text file and read all text, then it's up to you whether parse the JSON into objects or run an IndexOf on entire string.

Here's how to read text from a file,

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());
}
Naeem A. Malik
  • 995
  • 4
  • 19
  • no how do i gett all school which name is "Middle Schools" from textfile? – user2867267 Oct 12 '13 at 06:37
  • see my code above what what things i change to run this code get value from textfile of all school which name is "Middle Schools" – user2867267 Oct 12 '13 at 06:38
  • which method is fastest supose my school list more then 1000 so geting sall school in textfile and serch localy is fastest or insert all 1000 school in database and then getting school by name is fastest? – user2867267 Oct 12 '13 at 06:43
  • @user2867267 if your database is indexed well i think searching through the database would be the fastest. :) – Rat-a-tat-a-tat Ratatouille Oct 12 '13 at 06:53
  • @DharaShah is right, put your stuff in DB, it will make subsequent searches easy. – Naeem A. Malik Oct 12 '13 at 07:00
  • For string pattern matching, please see this SO topic: http://stackoverflow.com/questions/600733/using-java-to-find-substring-of-a-bigger-string-using-regular-expression – Naeem A. Malik Oct 12 '13 at 07:03
  • i need help see this post http://stackoverflow.com/questions/13814503/reading-a-json-file-in-android hwat i write at this line JSONObject obj = new JSONObject(json_return_by_the_function); – user2867267 Oct 12 '13 at 07:26
  • json_return_by_the_function what is it??????? – user2867267 Oct 12 '13 at 07:27
  • buddy Assets are used when we want to ship a non-changing file with the app, for JSON returned at runtime you can use app internal storage. Take some time, do some research about JSON; read following docs link carefully: http://developer.android.com/reference/org/json/JSONObject.html – Naeem A. Malik Oct 12 '13 at 08:09
0

When working with web services, it is recommended, that you maintain a local and persistent storage, for offline access.

Go through this tutorial: http://androidituts.com/android-sqlite-database-tutorial/

Only the part you need to change is, instead of:

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

You just need to write the insert query of database, which is also mentioned in the above tutorial.

Hope my recommendation suits you need...

Chintan Soni
  • 24,761
  • 25
  • 106
  • 174