Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php服务填充Android listview?_Php_Android_Json_Listview - Fatal编程技术网

如何使用php服务填充Android listview?

如何使用php服务填充Android listview?,php,android,json,listview,Php,Android,Json,Listview,现在我有一个数据库表“Content”,其中有“title”和“body”列 我制作了一个PHP rest服务来生成jSON,它可以读取 {"title":"bbhi"}{"title":"jhene aiko and "}{"title":"123456789101112"}{"title":"cats"}{"title":"drizzy"} 我现在只是想得到这个头衔。它确实成功地获取了我数据库的所有内容。我不确定这是否是我需要使用的jSON的正确形式 这是相关的PHP: $sql = "S

现在我有一个数据库表“Content”,其中有“title”和“body”列

我制作了一个PHP rest服务来生成jSON,它可以读取

{"title":"bbhi"}{"title":"jhene aiko and "}{"title":"123456789101112"}{"title":"cats"}{"title":"drizzy"}
我现在只是想得到这个头衔。它确实成功地获取了我数据库的所有内容。我不确定这是否是我需要使用的jSON的正确形式

这是相关的PHP:

$sql = "SELECT title FROM Content ORDER BY id DESC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $response["title"] = $row["title"];
        print(json_encode($response));
    }
    $response["success"] = 1;
} else {
    $response["success"] = 0;
    print(null);
}
现在,在Android方面,我重新设计了一个类似的类,用于登录用户。它使用GET查询URL,并(应该返回)jSON对象。在底部,我现在就填充了Android ListView(但使用同样的东西,它会一次又一次地重复)

我需要能够在jSON对象中包含所有标题,并且我希望能够遍历所有标题并填充我的android listview

这是获取jSON的相关Android类:

getNotes.java

类getNotes扩展异步任务{
JSONParser JSONParser=新的JSONParser();
私人对话;
私有静态最终字符串登录\u URL=”http://rohanharrison.com/message/androidGetNoteList.php";
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串标记_MESSAGE=“MESSAGE”;
私有静态最终字符串标记_TITLE=“TITLE”;
@凌驾
受保护的void onPreExecute(){
pDialog=新建进度对话框(MainActivity.this);
setMessage(“获取注释…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的JSONObject doInBackground(字符串…args){
试一试{
HashMap params=新的HashMap();
//参数put(“名称”,参数[0]);
//参数put(“密码”,参数[1]);
日志d(“请求”、“启动”);
JSONObject json=jsonParser.makeHttpRequest(
登录(URL,“获取”,参数);
if(json!=null){
Log.d(“JSON结果”,JSON.toString());
返回json;
}
}捕获(例外e){
e、 printStackTrace();
}
返回null;
}
受保护的void onPostExecute(JSONObject json){
int成功=0;
字符串消息=”;
ArrayList al=新的ArrayList();
if(pDialog!=null&&pDialog.isShowing()){
pDialog.disclose();
}
if(json!=null){
Toast.makeText(MainActivity.this,json.toString(),
Toast.LENGTH_LONG).show();
试一试{
success=json.getInt(TAG_success);
message=json.getString(TAG_message);
}捕获(JSONException e){
e、 printStackTrace();
}
}
if(json!=null){
Log.d(“成功!”,消息);
最终ListView lv=(ListView)findViewById(R.id.notesList);
String[]noteTitle=新字符串[0];
试一试{
对于(int i=0;i<24;i++){
al.add(json.getString(TAG_TITLE.toString());
}
}捕获(JSONException e){
e、 printStackTrace();
}
最终列表注释\列表=新阵列列表(al);
final ArrayAdapter ArrayAdapter=新的ArrayAdapter(MainActivity.this,android.R.layout.simple\u list\u item\u 1,notes\u list);
低压设置适配器(阵列适配器);
}否则{
Log.d(“故障”,消息);
}
}
}
对于可视化,这是数据库:

这就是Android ListView的外观:


一般来说,我对Android开发和jSON都是新手,所以我很抱歉我的无知。

您的视觉图像链接不适用于最佳实践,无法在jSON数组中生成标题或任何jSON对象。不确定imgur有什么问题…我将尝试将其设置为jSON数组,谢谢。您的可视图像链接不适用于最佳实践,请在json数组中生成标题或任何json对象。不确定imgur有什么问题…我将尝试将其设置为json数组,谢谢。
class getNotes extends AsyncTask<String, String, JSONObject> {

JSONParser jsonParser = new JSONParser();

private ProgressDialog pDialog;

private static final String LOGIN_URL = "http://rohanharrison.com/message/androidGetNoteList.php";

private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
private static final String TAG_TITLE = "title";

@Override
protected void onPreExecute() {
    pDialog = new ProgressDialog(MainActivity.this);
    pDialog.setMessage("Fetching Notes...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(true);
    pDialog.show();
}

@Override
protected JSONObject doInBackground(String... args) {

    try {

        HashMap<String, String> params = new HashMap<>();
        //params.put("name", args[0]);
        //params.put("password", args[1]);

        Log.d("request", "starting");

        JSONObject json = jsonParser.makeHttpRequest(
                LOGIN_URL, "GET", params);



        if (json != null) {
            Log.d("JSON result", json.toString());

            return json;
        }

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

    return null;
}

protected void onPostExecute(JSONObject json) {

    int success = 0;
    String message = "";
    ArrayList al = new ArrayList();

    if (pDialog != null && pDialog.isShowing()) {
        pDialog.dismiss();
    }

    if (json != null) {
        Toast.makeText(MainActivity.this, json.toString(),
                Toast.LENGTH_LONG).show();

        try {
            success = json.getInt(TAG_SUCCESS);
            message = json.getString(TAG_MESSAGE);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    if (json != null) {
        Log.d("Success!", message);

        final ListView lv = (ListView) findViewById(R.id.notesList);


        String[] noteTitle = new String[0];
        try {
            for (int i = 0; i < 24; i++) {
                al.add(json.getString(TAG_TITLE).toString());
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        final List<String> notes_list = new ArrayList<String>(al);
        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String> (MainActivity.this, android.R.layout.simple_list_item_1, notes_list);
        lv.setAdapter(arrayAdapter);



    } else {
        Log.d("Failure", message);
    }
}

}