gpt4 book ai didi

android - 将来自服务器的数据以 JSON 形式存储到 Android Sqlite 数据库中

转载 作者:太空狗 更新时间:2023-10-29 16:40:05 25 4
gpt4 key购买 nike

我从未在 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. 在第一类中,状态名称显示在 View 上。2. 在第二类中,根据上一类中选择的州,相应的城市名称显示在 View 上。3. 在第三类中,所选州和所选城市的组合 url 提供与该特定城市相关的详细信息。

所有数据均来自服务器

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

感谢任何帮助。

最佳答案

 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;

}

祝你好运......

关于android - 将来自服务器的数据以 JSON 形式存储到 Android Sqlite 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19241162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com