gpt4 book ai didi

android - Android学习应用程序存储数据的方法有哪些

转载 作者:行者123 更新时间:2023-11-29 17:23:27 28 4
gpt4 key购买 nike

我将编写一个允许学习语言编程的 Android 应用程序。一个例子,它是 PHP:我的应用程序将具有两个功能:

使用函数 A,我为每篇文章创建 html 页面,将 html 页面存储在项目中,然后通过 webview 显示它,就像这样:

webView.loadUrl("file:///android_asset/" + value);

注意:值是存储在 Assets 文件夹中的 html 文件。

有了功能B,就有那么多的问答。创建 html 文件太长了。我考虑过 XML 或 JSON 格式。这样做合适吗?您还有其他更好的解决方案吗?

p/s:抱歉关于标签,我不能用我的声誉做完整的标签。

最佳答案

基于:
Home » Android JSON Parsing – Retrieve From MySQL DatabaseAndroid JSON Parsing
从 2015 年 4 月 20 日起,作者:Belal Khan

您可以将数据存储在 MySQL 数据库中,并使用 PHP JSON 加载它。
例如对JSON进行编码的PHP文件为:

<?php
define('HOST','localhost');
define('USER','user');
define('PASS','pass');
define('DB','dbname');
$con = mysqli_connect(HOST,USER,PASS,DB);
$sql = "select * from questions";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array('id'=>$row[0],
'question'=>$row[1],
'answer'=>$row[2]
));
}
echo json_encode(array("result"=>$result));
mysqli_close($con);
?>

Java代码是:

public class MainActivity extends ActionBarActivity {

String myJSON;

private static final String TAG_RESULTS="result";
private static final String TAG_ID = "id";
private static final String TAG_QUESTION = "question";
private static final String TAG_ANSWER ="answer";

JSONArray peoples = null;

ArrayList<HashMap<String, String>> QuestionList;

ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView);
QuestionList = new ArrayList<HashMap<String,String>>();
getData();
}


protected void showList(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
results = jsonObj.getJSONArray(TAG_RESULTS);

for(int i=0;i<results.length();i++){
JSONObject c = peoples.getJSONObject(i);
String id = c.getString(TAG_ID);
String question = c.getString(TAG_QUESTION);
String answer = c.getString(TAG_ANSWER);

HashMap<String,String> questions = new HashMap<String,String>();

questions.put(TAG_ID,id);
questions.put(TAG_QUESTION,question);
questions.put(TAG_ANSWER,answer);

QuestionList.add(questions);
}

ListAdapter adapter = new SimpleAdapter(
MainActivity.this, personList, R.layout.list_item,
new String[]{TAG_ID,TAG_QUESTION,TAG_ANSWER},
new int[]{R.id.id, R.id.question, R.id.answer}
);

list.setAdapter(adapter);

} catch (JSONException e) {
e.printStackTrace();
}

}

public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String>{

@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://10.0.2.2/get-data.php");

// Depends on your web service
httppost.setHeader("Content-type", "application/json");

InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();

String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}

@Override
protected void onPostExecute(String result){
myJSON=result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}

你必须在你的布局中添加一个 ListView 并且不要忘记在 list 中添加互联网权限。

关于android - Android学习应用程序存储数据的方法有哪些,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35677414/

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