【问题标题】:Storing data coming from the server in form of JSON onto Android Sqlite database将来自服务器的数据以 JSON 的形式存储到 Android Sqlite 数据库中
【发布时间】:2013-10-14 23:45:34
【问题描述】:

我从未在 SQlite android 数据库上工作过。因此,如果可能的话,我将非常感谢这个问题中的一些勺子喂食/指针。

我有一项服务,它以JSON 的形式为我提供了一个状态名称列表,我需要将其解析并存储到 android 的数据库中,并将其显示到 ListView 上。 目前,我成功地能够解析 JSON 数据,然后借助以下代码将其直接显示到 ListView 上:

public class OpenMeetingsListActivity extends ListActivity 
{

    private Context context;
    private static String url = "http://myurl.com/mycountry/statelist.php";

    private static final String TAG_POSTS = "posts";
    private static final String TAG_SN = "sn";
    JSONArray posts = null;

    ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();

    ListView lv ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
        setContentView(R.layout.main);
        TextView t=(TextView)findViewById(R.id.textView_header);
        t.setText("Select Your State");

        new ProgressTask(OpenMeetingsListActivity.this).execute();

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        String stateSelected = ((TextView) v.findViewById(R.id.sn)).getText().toString();

        String url="http://myurl.com/mycountry/citylist.php?stname="+URLEncoder.encode(stateSelected);

        Intent intent=new Intent(OpenMeetingsListActivity.this,OpenMeetingsListActivity_Items.class);
        intent.putExtra("url", url);
        intent.putExtra("stateSelected",stateSelected);
        startActivity(intent);
    }

    private class ProgressTask extends AsyncTask<String, Void, Boolean> {

        private ProgressDialog dialog;
        OpenMeetingsListActivity op;


        public ProgressTask(ListActivity activity) {

            Log.i("1", "Called");
            context = activity;
            dialog = new ProgressDialog(context);
        }

        /** progress dialog to show user that the backup is processing. */

        /** application context. */
        private Context context;

        protected void onPreExecute() {
            this.dialog.setMessage("Progress start");
            this.dialog.show();
        }

        @Override
        protected void onPostExecute(final Boolean success) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }

            ListAdapter adapter = new SimpleAdapter(context, jsonlist,
                    R.layout.list_openmeeting_item, new String[] { TAG_SN }, new int[] {
                    R.id.sn });

            setListAdapter(adapter);

            // selecting single ListView item
            lv = getListView(); 

        }

        protected Boolean doInBackground(final String... args) {

            JSONParser jParser = new JSONParser();

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


            try {
                posts = json.getJSONArray(TAG_POSTS);
            } catch (JSONException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try
            {

                for(int i = 0; i < posts.length(); i++){
                    JSONObject c = posts.getJSONObject(i);
                    String sn = c.getString(TAG_SN);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(TAG_SN, sn);
                    jsonlist.add(map);
                } }catch (JSONException e) 
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            return null;

        }

    }



}

现在我需要在此代码中进行的更改或说明我需要添加到此代码的内容是我需要存储来自链接的 JSON 数据

http://myurl.com/mycountry/statelist.php

首先进入 android 中的数据库。然后我需要从数据库中获取该数据并将其显示到android中的ListView

完成后,我需要再次解析一个 url,例如

http://myurl.com/mycountry/citylist.php?stname="+URLEncoder.encode(stateSelected)

在下一堂课中显示各个州的各个城市(您在上面的班级中选择的州)。上面的 url 以 JSON 的形式给出了城市的名称,需要再次解析并存储到数据库中并显示到 ListView 上。

最后,当单击 ListView 中的城市名称时,它会重定向到第三个也是最后一个类,该类在下面形成此 url

http://myurl.com/mycountry/openmeet.php?reg="+state+"&cityvalid="+city

使用前两个类提供的信息。此 url 提供了一些 JSON 数据,这些数据再次需要解析、存储和显示到 ListView

因此我的应用程序有三个类: 1. 在第一类中,状态名称显示在视图上。 2. 在第二类中,各个城市名称显示在视图上,具体取决于在上一类中选择了哪个州。 3. 在第三类中,所选州和所选城市的组合 url 提供了与该特定城市相关的信息详细信息。

所有数据都来自服务器

我已经能够通过简单地将直接来自服务器的数据显示到ListView 上来实现此功能。现在我需要先将它存储到数据库中,然后将其显示在ListView 上,用于所有三个类。

感谢任何帮助。

【问题讨论】:

    标签: android json sqlite parsing listview


    【解决方案1】:
     protected Boolean doInBackground(final String... args) {
    
                JSONParser jParser = new JSONParser();
    
                // getting JSON string from URL
                JSONObject json = jParser.getJSONFromUrl(url);
    
    
                try {
                    posts = json.getJSONArray(TAG_POSTS);
                } catch (JSONException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                try
                {
    
                    for(int i = 0; i < posts.length(); i++){
                        JSONObject c = posts.getJSONObject(i);
                        String sn = c.getString(TAG_SN);
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put(TAG_SN, sn);
                        jsonlist.add(map);
    
                        //Just Put your database insert query here
                        ContentValues contentValues = new ContentValues();
                        contentValues.put(TAG_SN, sn);
                        database.insert(TABLE_NAME, null, contentValues);
    
                    } }catch (JSONException e) 
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
    
    
                return null;
    
            }
    

    祝你好运……

    【讨论】:

      【解决方案2】:

      我已经能够通过简单地显示来实现这个功能 直接从服务器传来的数据到 ListView 上。 现在我需要先将其存储到数据库中,然后将其显示在 ListView 用于所有三个类。

      恕我直言,您只需要知道 android sqlite CRUD 操作是如何工作的,以下是您可以采取的一些步骤来解决问题:

      • Google 一个 sqlite-android 教程
      • AFAIK,您了解 解析 json,因此您可以从 json 文件中获取数据(名称-值对)
      • 这些名称将成为sqlite db中的列
      • json 数据中的名称 关联的 将是列中的数据集
      • 要将数据再次填充到 ListView,您必须从 sqlite 获取(读取)数据,然后使用 Asynctask 再次填充它

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-08
        • 1970-01-01
        • 2021-07-11
        • 2016-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多