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

时间:2013-10-11 09:25:14

标签: android

我的问题是我正在获取数据表格url这是json格式我的应用程序在线现在我想让我的应用程序离线所以我将如何从本地数据库获取网址?我混淆了如何使应用程序脱机可以在数据库中插入所有json表单url然后使用相同的查询从本地数据库中获取json ???我的应用程序应用程序acxtivities有相同的类型类从url获取数据我将如何将我的应用程序设置为脱机?获取数据只有1次然后使用本地数据库???在我的网址中看到关于schoolId schoollevel的一些条件因此我将如何使用本地数据库来解决这个问题让我感到困惑帮助我如何使我的应用程序脱机

      {"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);
        }
    }

1 个答案:

答案 0 :(得分:0)

在安装时在sdcard中创建数据的文本文件(JSON OR 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()]);
希望这会对你有所帮助......