0

I'm getting data form url which is in json format my app is online now and I want to convert it to offline, So how will I get url from local database? How to make the application offline is it possible to insert all json form url into the database and then fetch it from local database using same query ? My application activities is having the same type classes get data from url. I'm geting data only once then use a local database? In my url there are some condition with respect to schoolId school leveled so how will I do this with the local database I'm confused to help me to make my application offline {"status":1,"data":[{"title":"Atypical"}]}

public class MenuGroup extends Activity {

    String status;
    Menugroupscreenadapter mma;
    String SelectMenuAPI;
    String SelectMenuAPI2;
     // String School_name;
    String elementry;
    String schedule_id, semestertitle, start_date, end_date;


    ProgressBar prgLoading;
    TextView txtAlert ,NoMenuAvailable ;
    int IOConnect = 0;
    ListView     listmenusgroups;
    String School_name;
    long School_ID;
    String URL,URL2;
    String SchoolLevelId;
    String menutype;
    TextView Elementarytxt, Middletxt, Hightxt, Atypicaltxt;
    static ArrayList<Long> Category_ID = new ArrayList<Long>();
    static ArrayList<String> Category_name = new ArrayList<String>();

    static ArrayList<Long> Menu_ID = new ArrayList<Long>();

    String _response;

    String Elementarytxtview,Middletextview,Hightextview,Atipicaltextview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menugroup);



        Intent iGet = getIntent();




           listmenusgroups = (ListView) findViewById(R.id.listmenusgroups);


        mma = new Menugroupscreenadapter(this);
          new getDataTask().execute();



          listmenusgroups.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                // TODO Auto-generated method stub
                Intent iMenuList = new Intent(MenuGroup.this, thirdstep.class);


                iMenuList.putExtra("Menu_ID", Menu_ID.get(position));
                iMenuList.putExtra("menu_group", Category_name.get(position));




                startActivity(iMenuList);

            }
        });

    }








     public class getDataTask extends AsyncTask<Void, String, String>{

            getDataTask(){
                if(!prgLoading.isShown()){


                }
            }

            @Override
             protected void onPreExecute() {
              // TODO Auto-generated method stub

            }

            @Override
            protected String doInBackground(Void... arg0) {
                // TODO Auto-generated method stub
        SelectMenuAPI = Utils.menugroups+School_ID+"&lid="+SchoolLevelId+"&menu=no";




                URL = SelectMenuAPI;
                URL2 = URL.replace(" ", "%20");


         try {

            Log.i("url",""+URL2);
             HttpClient client = new DefaultHttpClient();
             HttpGet request = new HttpGet(URL2); 
             HttpResponse response = client.execute(request);
             HttpEntity resEntity = response.getEntity();
              _response=EntityUtils.toString(resEntity);






       }

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



                          return _response;
            }

            @Override
            protected void onPostExecute(String result) {
                // TODO Auto-generated method stub
                prgLoading.setVisibility(8);


                    listmenusgroups.setVisibility(0);


                    try {




            JSONObject json2 = new JSONObject(result);

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



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

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

         JSONObject object = school.getJSONObject(i);



               Category_ID.add((long) i);


         Menu_ID.add(Long.parseLong(object.getString("menu_id")));

          Category_name.add(object.getString("menu_title"));

        }

        } 
        else {
        NoMenuAvailable.setVisibility(View.VISIBLE);
        NoMenuAvailable.setText("No Menu Available for this School.");
        listmenusgroups.setVisibility(View.GONE);


            }

            } 

        catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
                    }
        }else{
        txtAlert.setVisibility(0);
        }
        listmenusgroups.setAdapter(mma);
            }
        }
4

1 回答 1

0

安装时在 sdcard 中创建数据(JSON 或 XML)的文本文件,并在没有互联网连接时将该文本文件作为输入。

通过遵循这一点,您可以实现您的目标。

    File storage = new File("/sdcard/appData/photos");
    storage.mkdirs();

    JSONParser jParser = new JSONParser();

    // getting JSON from URL
    JSONObject json = jParser.getJSONFromUrl(url1);

    //transforming jsonObject to byte[] and store it
    String jsonString = json.toString();                
    byte[] jsonArray = jsonString.getBytes();

    String filen = "jsonData";
    File fileToSaveJson = new File("/sdcard/appData",filen);

    FileOutputStream fos;
    fos = new FileOutputStream(fileToSaveJson);

    fos = openFileOutput(filen,Context.MODE_PRIVATE);
    fos.write(jsonArray);
    fos.close();


    //re

ading jsonString from storage and transform it into jsonObject              
FileInputStream fis;
File readFromJson = new File("/sdcard/appData/jsonData");

fis =  new FileInputStream(readFromJson);

fis =  new FileInputStream(readFromJson);
InputStreamReader isr = new InputStreamReader(fis);

fis.read(new byte[(int)readFromJson.length()]);

希望这可以帮助...

于 2013-10-11T09:48:10.903 回答