Android Volley Auth令牌问题发布请求

时间:2019-03-15 07:26:48

标签: java android android-volley

我尝试了很多想法,观看了无数的youtube视频,在Google上进行了搜索...

这是我第一次必须连接到后端服务器。

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<div class="container">
  <div class="row">
    <div class="col-lg-1">I am 64px width block</div>
    <!-- /.col-xl-1 -->
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

尝试

public class MainActivity extends AppCompatActivity {
private String token;
private String email;
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RequestQueue requestQueue;
private List<ListItem> listItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = findViewById(R.id.recyclerview);
    recyclerView.setHasFixedSize(true);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(linearLayoutManager);
    Submit();

}
private void Submit() {


    requestQueue = Volley.newRequestQueue((getApplicationContext()));
    StringRequest postRequest = new StringRequest(Request.Method.POST, URL_DATA + "api-user-get",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {



                    try {
                        JSONObject parentObject = new JSONObject(response);
                        JSONObject childObject = parentObject.getJSONObject("result");
                        token=childObject.getString("token");

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

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

                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
    ) {

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("email", "t1@gmail.com");

            return params;
        }


    };
    requestQueue.add(postRequest);
}








private void loadRecyclerViewData(String token){

            StringRequest listRequest = new StringRequest(Request.Method.POST,URL_DATA+"api-spot-get-all",
                    new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        JSONArray array = jsonObject.getJSONArray("result");
                        for (int i =0; i<array.length(); i++) {
                            JSONObject o = array.getJSONObject(i);
                            ListItem list = new ListItem(
                                    o.getString("id"),
                                    o.getString("name"),
                                    o.getString("country"),
                                    o.getString("whenToGo"),
                                    o.getBoolean("isFavorite")
                            );
                            listItems.add(list);
                        }
                        adapter = new ListItemAdapter(getApplicationContext(), listItems);
                        recyclerView.setAdapter(adapter);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
 //
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }){

                @Override
                public Map<String, String> getHeaders() {
                    Map<String, String> headers = new HashMap<String, String>();
                    headers.put("Content-Type", "application/json");
                    headers.put("token", token);
                    return headers;
                }



                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String,  String>();
                    params.put("country", "");
                    params.put("windProbability", "");
                    return params;
                }
};


    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(listRequest);


}



}