Rusty Ma
Rusty Ma

Reputation: 23

Android Volley Params

In my project, i pass the firstname of a user in a params from the JSONobject request. It would then get the response and fill the textviews. however i cant figure out why my code does not work.I checked my php and it works fine when i put a predefined firstname in it, so i ruled out a web service problem. does it get the response first and then pass the params? please help

public class ProfileActivity extends AppCompatActivity {

TextView Username, Firstname, Lastname, Birthdate, Barangay;

String firstname;
String json_url = "http://localhost/android/getprofileinfo.php";



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    Username = (TextView)findViewById(R.id.usernameprofile);
    Firstname = (TextView)findViewById(R.id.firstnameprofile);
    Lastname = (TextView)findViewById(R.id.lastnameprofile);
    Birthdate = (TextView)findViewById(R.id.birthdayprofile);
    Barangay = (TextView)findViewById(R.id.barangayprofile);

   final Bundle bundle = getIntent().getExtras();

    firstname = bundle.getString(firstname);





    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, json_url, (String) null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        Username.setText(response.getString("username"));
                        Firstname.setText(response.getString("firstname"));
                        Lastname.setText(response.getString("lastname"));
                        Birthdate.setText(response.getString("birthdate"));
                        Barangay.setText(response.getString("barangay"));
                    }

                    catch(JSONException e){
                            e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            Toast.makeText(ProfileActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
            error.printStackTrace();
        } //end of method onErrorResponse

    })
    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("firstname", firstname);
            return params;
        }
    };
    MySingleton.getmInstance(ProfileActivity.this).addTorequestque(jsonObjectRequest);

}

}

this is the getpropileinfo.php

<?php

$firstname =$_POST["firstname"];


define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','mydb');

$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');

$sql = "SELECT username,firstname,lastname,birthdate,barangay FROM users 
WHERE firstname LIKE '".$firstname."'; ";

$result = mysqli_query($con, $sql);

if(mysqli_num_rows($result) > 0)
{
$row = mysqli_fetch_assoc($result);
echo json_encode(array("username"=>$row['username'], 
"firstname"=>$row['firstname'], 
    "lastname"=>$row['lastname'], "birthdate"=>$row['birthdate'], 
"barangay"=>$row['barangay']));
}
?>

Upvotes: 2

Views: 56

Answers (4)

Jhungamesh
Jhungamesh

Reputation: 5

Change your JSONRequest to a StringRequest.

Upvotes: 0

Navneet Krishna
Navneet Krishna

Reputation: 5017

You are not properly taking the getExtras(), provide the key name which you pass using putExtra() from your calling activity, like this

In the calling activity pass intent like

Intent i = new Intent(FirstActivity.this, ProfileActivity.class);   
String strName = "some_name";
i.putExtra("key_username", strName);
startActivity(i);

Then in ProfileActivity,

final Bundle bundle = getIntent().getExtras();
firstname = bundle.getString("key_username");

Convert to JsonArrayRequest

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, json_url, (String) null,
            new com.android.volley.Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    try {

                            JSONObject person = (JSONObject) response
                                    .get(0);

                            Username.setText(person.getString("username"));
                            Firstname.setText(person.getString("firstname"));
                            Lastname.setText(person.getString("lastname"));
                            Birthdate.setText(person.getString("birthdate"));
                            Barangay.setText(person.getString("barangay"));



                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(),
                                "Error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }

                }
            }, new com.android.volley.Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            Toast.makeText(ProfileActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
            error.printStackTrace();
        } //end of method onErrorResponse

    })
    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("firstname", firstname);
            return params;
        }
    };

Upvotes: 1

Alaa AbuZarifa
Alaa AbuZarifa

Reputation: 1249

Inside the method parameters, remove the string casting for the json object.

null instead of (String) null.

Upvotes: 0

PRATEEK BHARDWAJ
PRATEEK BHARDWAJ

Reputation: 2432

May be its issue with your localhost, Go through Genymotion is using your PC IP address. to get your IP address go to:

start -> cmd -> ipconfig then search for IPv4, copy the IP and paste it in your URL. It should looks like the following:

String YourURL = "http://192.168.0.106:8888/android/getprofileinfo.php";

Hope this works too for you.

Upvotes: 0

Related Questions