答案 0 :(得分:1)
执行此操作。添加标头数据。
RequestQueue queue = Volley.newRequestQueue(this);
String url = APICLient.BASE_URL+"api/Admin_controller/getProspectField";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jo=new JSONObject(response);
Boolean status=jo.getBoolean("success");
JSONArray ja=jo.getJSONArray("user");
// write your logic here...
for (int i=0;i<ja.length();i++){
JSONObject jsonObject=ja.getJSONObject(i);
String pname=jsonObject.getString("p_name");
String pType=jsonObject.getString("p_type");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}) {
@Override
public Map<String, String> getHeaders(){
HashMap <String,String> params=new HashMap<>();
params.put("authorization","put_authorization_key_here");
return params;
}
@Override
public Map<String, String> getParams(){
HashMap <String,String> params=new HashMap<>();
params.put("care_team[]",team);
........................
return params;
}
};
queue.add(stringRequest);
希望这会起作用。
答案 1 :(得分:0)
Json用截击进行解析:
private void userLogin() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, URLs.URL_LOGIN,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
//converting response to json object
JSONObject obj = new JSONObject(response);
//if no error in response
if (obj.getBoolean("status")) {
Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
//getting the user from the response
JSONObject userJson = obj.getJSONObject("user");
//creating a new user object
User user = new User(
userJson.getInt("cid"),
userJson.getInt("company_id"),
userJson.getString("name"),
userJson.getString("email")
);
//storing the user in shared preferences
SharedPreferenceManager.getInstance(getApplicationContext()).userLogin(user);
//starting the profile activity
finish();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
} else {
Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
} catch (JSONException e) {
e.printStackTrace();
progressBar.setVisibility(View.GONE);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("company_id",companyid );
params.put("email", username);
params.put("password", password);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("x-api-key", "swicons@1019");
return headers;
}
};
stringRequest.setShouldCache(false);
VolleySingleton.getInstance(this).addToRequestQueue(stringRequest);
}