matlabmann 2015-11-07 03:14
浏览 157

MySQL数据库到android ListView

I am trying to load data from MySQL database to android ListView. This one is the example I saw : http://codeoncloud.blogspot.com/2013/07/android-mysql-php-json-tutorial.html

Here's my code :

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;
}

}

I'm getting nothing in my android activity.

My connection.php output :

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

Please Help. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • duanguilin2007 2015-11-07 03:26
    关注

    It may because your doInBackground function is doesn't return anything.

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器