如何使用 url 将联机应用程序更改为脱机



我正在获取 json 格式的数据表单 url,我的应用程序现在在线,我想将其转换为离线,那么我将如何从本地数据库获取 url?如何使应用程序脱机是否可以将所有json表单URL插入数据库,然后使用相同的查询从本地数据库获取它?我的应用程序活动是让相同类型的类从 url 获取数据。我只获取一次数据,然后使用本地数据库?在我的 url 中,有一些关于 schoolId 学校分级的条件,所以我将如何使用本地数据库执行此操作我很困惑以帮助我使我的应用程序离线 {"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);
            }
        }

在安装时在SD卡中创建数据(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()]);

希望这有帮助...