0

Here's my Android code that first picks phone from another class:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
int defaultValue = 0;
phone = prefs.getString("Number", String.valueOf(defaultValue));

then trying to post it by volley like this:

private void getStatus(final String phone) {
    final String URL = Config.DATA_URL_STATUS;
    // Post params to be sent to the server
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("phone",phone);

    loading = ProgressDialog.show(this, "Please wait...", "Validating...", false, false);
    JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        VolleyLog.v("Response:%n %s", response.toString(4));
                        showJSONE(response);
                        loading.hide();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.e("Error: ", error.getMessage());
            loading.hide();
        }
    });

    // add the request object to the queue to be executed
    AppController.getInstance().addToRequestQueue(req);
}

My php code:

$con = mysqli_connect(HOST,USER,PASS,DB); 
$phone = $_POST['phone'];
$sql = "SELECT * FROM transactions ";
$sql .= "WHERE sender_phone='$phone'";
$sql .= "ORDER BY id DESC LIMIT 1";

The problem is that this code doesn't pick the value for $phone

Misa Lazovic
  • 2,805
  • 10
  • 32
  • 38
Davinski
  • 3
  • 2
  • 1
    Shared preference in android specific file and it available in the android context. Why do you want to send that to php? Can you elaborate your quesiton? Instead you can read data from shared preferece and post to server – Deepak M Oct 11 '16 at 07:28
  • You're sending the phone as a JSON object, but then you're trying to get the value as a form-encoded POST parameter. See this answer https://stackoverflow.com/questions/19004783/reading-json-post-using-php/19007851#19007851 – Xavier Rubio Jansana Oct 11 '16 at 07:36
  • thanks for the quick feedback, I'm still new at this, Deepak M could you give me a hint as to how to post a shared preference value to a server. As for Xavier Rubio thanks for helping me see that php has to obtain the value not as a form-encoded POST parameter. Reviewing your example. – Davinski Oct 11 '16 at 08:22
  • `how to post a shared preference value to a server.`. Well you keep talking about shared preferences but shared preferences has nothing to do with your problem. You just want to send a string value to php. So all you want to know is how to send parameters to php. Well more basic there is not. You can find code for it everery day again on stackoverflow. – greenapps Oct 11 '16 at 09:15
  • greenapps you're correct......i used Xavier's link to solve it. Thanks a lot guys! – Davinski Oct 11 '16 at 09:57

0 Answers0