基于:
Home » Android JSON Parsing – Retrieve From MySQL Database Android JSON Parsing
从2015年4月20日,由贝拉尔汗
您可以将您的数据在一个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();
}
你必须在你的布局 添加列表视图,不要忘了在清单中添加Internet权限。
为什么创建HTML文件太长? HTML文件是否有限制? – user3793589
@ user3793589:如果我有100个问题和答案,是否需要很长时间才能创建html文件?而且我会得到一些未知的记忆问题。 – kidsoul
所以基本上你正在创建一个网站,并在应用程序中显示。这可能看起来不太好,或者看起来像一个Android应用程序!我的建议是忘掉HTML,考虑真正的课程设计指南(章节,标题,标题......),然后创建你自己的格式来存储这个结构化的内容(我会去与SQLite或Json,压缩)。如果你做对了,外观和感觉会更好! – personne3000