单例json从数据库获取数据到我的应用

时间:2018-12-02 17:47:00

标签: java android mysql android-studio

我做完作业,被告知要显示从数据库到Android应用程序的数据,但是我的代码不会仅显示空白页

这是我的xml

<LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">

        <Button
            android:id="@+id/btn_activity_main_pindah"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/texttotal"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="#7ec6ef"
            android:text="Pindah"
            android:onClick="Pindah"
            />

    </LinearLayout>

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout"
        android:layout_centerHorizontal="true">

        <TextView
            android:id="@+id/txtDisplay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp" />
    </ScrollView>

这是我的java,我正在使用单例

String url= "http://testtest/bulanan.php?";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONArray jsonarray = new JSONArray(response);
                    StringBuilder textViewData = new StringBuilder();
                    //Parse the JSON response array by iterating over it
                    for (int i = 0; i < jsonarray.length(); i++) {
                        JSONObject jsonobject = jsonarray.getJSONObject(i);
                        String id = jsonobject.getString(KEY_ID);
                        String tipe = jsonobject.getString(KEY_TIPE);
                        String waktu = jsonobject.getString(KEY_WAKTU);
                        String totalltr = jsonobject.getString(KEY_TOTALLTR);
                        String total = jsonobject.getString(KEY_TOTAL);
                        //Create String out of the Parsed JSON
                        textViewData.append("id: ").append(id).append(NEW_LINE);
                        textViewData.append("tipe: ").append(tipe).append(NEW_LINE);
                        textViewData.append("waktu: ").append(waktu).append(NEW_LINE);
                        textViewData.append("totalltr: ").append(totalltr).append(NEW_LINE);
                        textViewData.append("total: ").append(total).append(NEW_LINE);
                        textViewData.append(NEW_LINE);
                    }
                    //Populate textView with the response
                    mTxtDisplay.setText(textViewData.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if(error != null){
                    Toast.makeText(getApplicationContext(), "Something went wrong.", Toast.LENGTH_LONG).show();
                }
            }
        }
);

MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest);

输出只是一个按钮,数组没有显示出来,我已经对其进行了更改,但是仍然无法正常工作,我的代码有问题吗?

请帮助

0 个答案:

没有答案