使用sessionaid附加json数组请求

时间:2017-10-04 19:51:47

标签: android android-volley

我发送请求,使用php脚本从mysql数据库中检索信息数组。起初它只使用了网址,但我很难将sessionID添加到JsonArrayRequest作为参数。如何在数组请求中添加参数sessionID

public class MainActivity extends AppCompatActivity {
    RequestQueue rq;
    String sessionID;
    TextView email, fullname , contact;
    String emailStr, fullnameStr, contactStr;
    String url = "http://kutso011.000webhostapp.com/RetrieveData.php";
    String jsonResponse;

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

        rq = Volley.newRequestQueue(this);

        email = (TextView)findViewById(R.id.txtemaill);
        fullname = (TextView)findViewById(R.id.txtFullnamee);
        contact = (TextView)findViewById(R.id.txtContactt);

    sessionID = getIntent().getStringExtra("UserID");
        sendjsonrequest();
    }

    private void sendjsonrequest() {
        JsonArrayRequest req = new JsonArrayRequest(url, sessionID,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        try {
                            // Parsing json array response
                            // loop through each json object
                            jsonResponse = "";
                            for (int i = 0; i < response.length(); i++) {
                                JSONObject person = (JSONObject) response
                                        .get(i);

                               String emailStr = person.getString("email");
                               String contactStr = person.getString("Contact");
                               String fullnameStr = person.getString("FullName");

                               email.setText(emailStr);
                               fullname.setText(fullnameStr);
                               contact.setText(contactStr);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error)
            {
            }
        });

        // Adding request to request queue
        //AppController.getInstance().addToRequestQueue(req);
        rq.add(req);
        //rq.add(jsonObjectRequest);
    }
}

1 个答案:

答案 0 :(得分:0)

public class Profile extends AppCompatActivity {

private String sessionID;
RequestQueue rq;
String jsonResponse;
String url = "http://kutso011.000webhostapp.com/October/retrieveData.php";
EditText fname_,lname_,contact_,email_;

public void initViews()
{
    fname_ = (EditText)findViewById(R.id.etFName);
    lname_ = (EditText)findViewById(R.id.etLName);
    contact_ = (EditText)findViewById(R.id.etContact);
    email_ = (EditText)findViewById(R.id.etMail);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    rq = Volley.newRequestQueue(this);
    initViews();
    sessionID = getIntent().getStringExtra("sessionID");



    sendjsonrequest(sessionID);

}

private void sendjsonrequest(final String sessionID)
{

    JsonArrayRequest req = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {


                    try
                    {
                        // Parsing json array response
                        // loop through each json object
                        jsonResponse = "";
                        for (int i = 0; i < response.length(); i++) {

                            JSONObject profileInfo = (JSONObject) response
                                    .get(i);


                            String fname = profileInfo.getString("FirstName"); //
                            String lname = profileInfo.getString("LastName");
                            String contact = profileInfo.getString("Contact");
                            String email = profileInfo.getString("Email");
                            Toast.makeText(getApplicationContext(), fname , Toast.LENGTH_LONG).show();

                            fname_.setText("fname");
                            lname_.setText(lname);
                            contact_.setText(contact);
                            email_.setText(email);

                        }

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

                    }

                }
            }, new Response.ErrorListener()
    {

        @Override
        public void onErrorResponse(VolleyError error)
        {
            Toast.makeText(getApplicationContext(),"Could not fetch data, connection error " , Toast.LENGTH_LONG).show();
        }


    }){
       @Override
        protected Map<String, String> getParams()
        {
            // Posting parameters to retrieve profile url
            Map<String, String> params = new HashMap<String, String>();
            params.put("sessionID", sessionID);


            return params;
        }
    };



    // Adding request to request queue
    //AppController.getInstance().addToRequestQueue(req);
    rq.add(req);
    //rq.add(jsonObjectRequest);
}

}