0

我正在创建一个离线工作的应用程序。我的应用程序在在线模式下运行良好并从 url 获取 json。我怎样才能让它离线工作?如何将 json 从 url 插入 SQLite?我想先从 url 获取所有数据,然后将其插入 SQLite,然后使用数据库。

这是我的完整代码:

public class MainActivity extends Activity {
CategoryListAdapter3 cla;
static ArrayList<Long> Category_ID = new ArrayList<Long>();
static ArrayList<String> Category_name = new ArrayList<String>();
static ArrayList<String> Category_image = new ArrayList<String>();
String URL, URL2;
String SelectMenuAPI;
String _response;
String status;
GridView gridview;

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

    gridview = (GridView) findViewById(R.id.gridview);
    cla = new CategoryListAdapter3(MainActivity.this);
    new TheTask().execute();

    gridview.setOnItemClickListener(new OnItemClickListener() {

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

            iMenuList.putExtra("Category_ID",  
                            Category_ID.get(position));
            iMenuList.putExtra("Category_name", 
                           Category_name.get(position));

            startActivity(iMenuList);

        }
    });

}

void clearData() {
    Category_ID.clear();
    Category_name.clear();
    Category_image.clear();

}

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

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected String doInBackground(Void... arg0) {

SelectMenuAPI = "http://www.syddddds.pk/beta7/_webservices 
                       /mobile_api.php?response=getmaincategories";

        clearData();
        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 (Exception e) {
            e.printStackTrace();
        }
        return _response;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        try {

            JSONObject json2 = new JSONObject(result);

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

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

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

  Category_ID.add(Long.parseLong(object .getString("category_id")));

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

  Category_image.add(object.getString("image_path"));

                }

            }

            else {



            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        gridview.setAdapter(cla);
        cla.notifyDataSetChanged();

    }
}





   public class CategoryListAdapter3 extends BaseAdapter {

private Activity activity;


private AQuery androidAQuery;


public CategoryListAdapter3(Activity act) {
    this.activity = act;
//  imageLoader = new ImageLoader(act);
}

public int getCount() {
    // TODO Auto-generated method stub
    return MainActivity.Category_ID.size();
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;
    androidAQuery = new AQuery(getcontext());
    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.viewitem2, null);
        holder = new ViewHolder();

        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }


    holder.txtText = (TextView) convertView.findViewById(R.id.title2);
    holder.imgThumb = (ImageView) convertView.findViewById(R.id.image2);

    holder.txtText.setText(MainActivity.Category_name.get(position));


   androidAQuery.id(holder.imgThumb).image(MainActivity.Category_image.get(position), 
    true, true);

    return convertView;
}
private Activity getcontext() {
    // TODO Auto-generated method stub
    return null;
}
static class ViewHolder {
    TextView txtText;
    ImageView imgThumb;
}

    }
4

0 回答 0