如何通过按钮从android中的数据库中检索数据

时间:2016-02-16 09:23:05

标签: android sqlite

我已经连接了android(客户端)和PHP(server)以便检索数据,并且当我单独执行时它成功但是通过提供按钮来连接我的主程序时,不检索数据。通过单击按钮它正在进行加载,然后模块页面将再次出现。

package com.example.sim;
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public void click(View v){
        Intent intent=new Intent(this,AainActivity.class);
        startActivity(intent);
    }
}

Aainactivity看起来像这样

public class SampleHttp extends AsyncTask<String, String, String> {

    private ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {

        pDialog = new ProgressDialog(AainActivity.this);
        pDialog.setMessage("Loading . Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();

        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub

        String result = null;
        InputStream is = null;
        try {
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://smart.prometheantechnologysolutions.com/s.php");
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

            Log.e("log_tag", "connection success");
            // Toast.makeText(getApplicationContext(), “pass”,
            // Toast.LENGTH_SHORT).show();
        } catch (Exception e) {


        }
        // convert response to string
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");

            }
            is.close();

            result = sb.toString();
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result" + e.toString());
            //Toast.makeText(getApplicationContext(), "Input reading fail", Toast.LENGTH_SHORT).show();
        }
        return result;
    }

    @Override
    protected void onPostExecute(String result) {
        try {
            JSONArray jArray = new JSONArray(result);
            TableLayout tv = (TableLayout) findViewById(R.id.table);
            tv.removeAllViewsInLayout();
            int flag = 1;
            for (int i = -1; i < jArray.length(); i++) {
                TableRow tr = new TableRow(AainActivity.this);
                tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                if (flag == 1) {
                    TextView b6 = new TextView(AainActivity.this);
                    b6.setText("Id");
                    b6.setTextColor(Color.BLUE);
                    b6.setTextSize(15);
                    tr.addView(b6);
                    TextView b19 = new TextView(AainActivity.this);
                    b19.setPadding(10, 0, 0, 0);
                    b19.setTextSize(15);
                    b19.setText("Name");
                    b19.setTextColor(Color.BLUE);
                    tr.addView(b19);
                    TextView b29 = new TextView(AainActivity.this);
                    b29.setPadding(10, 0, 0, 0);
                    b29.setText("Address");
                    b29.setTextColor(Color.BLUE);
                    b29.setTextSize(15);
                    tr.addView(b29);
                    tv.addView(tr);
                    final View vline = new View(AainActivity.this);
                    vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
                    vline.setBackgroundColor(Color.BLUE);
                    tv.addView(vline);
                    flag = 0;
                } else {
                    JSONObject json_data = jArray.getJSONObject(i);
                    Log.i("log_tag", "id: " + json_data.getInt("id") + ", Username: "
                            + json_data.getString("name") + ", No: " + json_data.getString("address"));
                    TextView b = new TextView(AainActivity.this);
                    String stime = String.valueOf(json_data.getInt("id"));
                    b.setText(stime);
                    b.setTextColor(Color.RED);
                    b.setTextSize(15);
                    tr.addView(b);
                    TextView b1 = new TextView(AainActivity.this);
                    b1.setPadding(10, 0, 0, 0);
                    b1.setTextSize(15);
                    String stime1 = json_data.getString("name");
                    b1.setText(stime1);
                    b1.setTextColor(Color.BLACK);
                    tr.addView(b1);
                    TextView b2 = new TextView(AainActivity.this);
                    b2.setPadding(10, 0, 0, 0);
                    String stime2 = json_data.getString("address");
                    b2.setText(stime2);
                    b2.setTextColor(Color.BLACK);
                    b2.setTextSize(15);
                    tr.addView(b2);
                    tv.addView(tr);
                    final View vline1 = new View(AainActivity.this);
                    vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
                    vline1.setBackgroundColor(Color.WHITE);
                    tv.addView(vline1);
                }
            }
        } catch (Exception e) {
            Log.e("log_tag", "Error parsing data" + e.toString());
            Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
        }

        pDialog.dismiss();
        super.onPostExecute(result);
    }
}

我做错了什么?我想通过按钮从数据库中检索数据。

1 个答案:

答案 0 :(得分:0)

首先确保您的休息请求正确无误。 I:E;您的服务器正在回复移动设备

如果休息请求成功。 假设您正在从服务器向机器人发送json响应。 验证您的json字符串响应here

它生成java类,将它们复制到项目中,并将gson库添加到gradle中。

你完成了。

Gson gson = new Gson();
gson.fromJson(jsonStr,YourClass.class);

它提供了您正在寻找的所有字段

如果您遇到任何问题,请在下方发表评论。