【问题标题】:MySQL database to android ListViewMySQL 数据库到 android ListView
【发布时间】:2015-11-07 03:14:27
【问题描述】:

我正在尝试将数据从 MySQL 数据库加载到 android ListView。 这是我看到的例子:http://codeoncloud.blogspot.com/2013/07/android-mysql-php-json-tutorial.html

这是我的代码:

public class AbCdEfGh extends ActionBarActivity {

private String jsonResult;
private String url = "http://merosong.byethost24.com/android_php_files_management/connection.php";
private ListView listView;
ProgressDialog pDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //showing a ProgressDialog
    pDialog = new ProgressDialog(this);
    pDialog = ProgressDialog.show(this, "", "Loading", true);

    setContentView(R.layout.activity_choose);
    listView = (ListView) findViewById(R.id.list_main);

    accessWebService();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present
    getMenuInflater().inflate(R.menu.menu_choose, menu);
    return true;
}

// Async Task to access the web
private class JsonReadTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(params[0]);
        try {
            HttpResponse response = httpclient.execute(httppost);
            jsonResult = inputStreamToString(
                    response.getEntity().getContent()).toString();

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

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

        }
        return null;


    }

    private StringBuilder inputStreamToString(InputStream is) {
        String rLine = "";
        StringBuilder answer = new StringBuilder();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        try {
            while ((rLine = rd.readLine()) != null) {
                answer.append(rLine);
            }
        } catch (IOException e) {


        }
        return answer;
    }

    @Override
    protected void onPostExecute(String result) {
        pDialog.dismiss();
        ListDrwaer();

    }



}// end async task

public void accessWebService() {
    JsonReadTask task = new JsonReadTask();
    // passes values for the urls string array
    task.execute(new String[]{url});
}

// build hash set for list view
public void ListDrwaer() {
    List<Map<String, String>> trackList = new ArrayList<Map<String, String>>();

    try {
        JSONObject jsonResponse = new JSONObject(jsonResult);
        JSONArray jsonMainNode = jsonResponse.optJSONArray("mstracks");
        for (int i = 0; i < jsonMainNode.length(); i++) {
            JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
            String name = jsonChildNode.optString("name");
            String outPut = name + "";
            trackList.add(createEmployee("merosongayush", outPut));
        }

    } catch (JSONException e) {

    }


    SimpleAdapter simpleAdapter = new SimpleAdapter(this, trackList,
            android.R.layout.simple_list_item_1,
            new String[]{"merosongayush"}, new int[]{android.R.id.text1});
    listView.setAdapter(simpleAdapter);

}

private HashMap<String, String> createEmployee(String name, String link) {
    HashMap<String, String> finalNameLink = new HashMap<String, String>();
    finalNameLink.put(name, link);
    return finalNameLink;
}

}

我的 android 活动一无所获。

我的 connection.php 输出:

http://merosong.byethost24.com/android_php_files_management/connection.php

请帮忙。提前致谢。

【问题讨论】:

    标签: php android mysql listview


    【解决方案1】:

    这可能是因为您的 doInBackground 函数没有返回任何内容。

    【讨论】:

    • 当我使用这个网址:cpriyankara.coolpage.biz/employee_details.php 从“emp_info”加载“员工姓名”时,一切正常。那我的问题是什么?
    • 这不是问题的原因,因为jsonResult 是 Activity 的成员变量。
    • 是的,对不起,我看到了。如果您的代码与 [link](cpriyankara.coolpage.biz/employee_details.php) 正常工作,我找不到任何错误
    猜你喜欢
    • 1970-01-01
    • 2013-03-23
    • 2018-10-08
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多