mercredi 24 février 2016

Android Studio - Request data from Server via PHP

I follow the link below to learn on how to POST and GET a data from Android Application using PHP with JSON and Save to SQLite database.

Android Login and Registration with PHP, MySQL and SQLite

It's working fine, i can POST and GET a data, but now i'm stock because i'trying to request all the data from my LOCALHOST server and then insert it to my SQLite database in android.

Here what i did at my PHP CODE:

$query = $con->prepare('SELECT * FROM '.TABLE.';');
                $query->bindValue(':id',$id,PDO::PARAM_INT);
                $query->execute();
                if($query->rowCount() > 0){
                    $data= $query->fetchAll();

                    //get all data here
                    foreach($data as $row) {
                        $response["error"] = FALSE;
                        $response["id"] = $row["id"];
                        $response["data"]["data1"] = $row["data1"];
                        $response["data"]["data2"] = $row["data2"];
                        $response["data"]["data3"] = $row["data3"];
                        $response["data"]["data4"] = $row["data4"];
                        $response["dat"]["data5"] = $row["data5"];
                        echo json_encode($response); //then response to android application
                    }
                }else{
                    $response["error"] = TRUE;
                    $response["error_msg"] = "No data...";
                    echo json_encode($response);
                }

and in my java I've just follow the tutorial so i got this:

 private void checkNotification(final String userid) {
        // Tag used to cancel the request
        String tag_string_req = "req_login";
        pDialog.setMessage("Checking Data ...");
        showDialog();
        StringRequest strReq = new StringRequest(Request.Method.POST,
                AppConfig.URL_NOTIFICATION, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d(TAG, "DATA Response: " + response.toString());
                hideDialog();
                try {
                    JSONObject jObj = new JSONObject(response);
                    boolean error = jObj.getBoolean("error");

                    // Check for error node in json
                    if (!error) {

                        /***************************************************/
                        //get all the data from server and save it to SQLite
                        String iid = jObj.getString("id");
                        JSONObject notif = jObj.getJSONObject("data");
                        String d1 = notif.getString("data1");
                        String d2 = notif.getString("data2");
                        String d3 = notif.getString("data3");
                        String d4 = notif.getString("data4");
                        String d5 = notif.getString("data5");
                        db.addData(iid, d1, d2, d3, d4, d5);//insert now here
                        /***************************************************/

                    } else {
                        // Error in login. Get the error message
                        String errorMsg = jObj.getString("error_msg");
                        Toast.makeText(getApplicationContext(),
                                errorMsg, Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    // JSON error
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Json errors: " + e.getMessage(), Toast.LENGTH_LONG).show();
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Fetching NOTIF Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_LONG).show();
                hideDialog();
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                // Posting parameters to login url
                Map<String, String> params = new HashMap<String, String>();
                params.put("id", userid);
                //params.put("password", password);
                return params;
            }
        };
        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }

i got no error and i check from my LOG, all the data is sending to application but unfortunately when i check to my SQLite database only one data is saved.

Can anyone help me please?

Thank you so much!!!

Aucun commentaire:

Enregistrer un commentaire