<span style="font-size:18px;">1.添加全局myapplication:</span><pre name="code" class="java">public class MyApplication extends Application {
public static RequestQueue queue;
@Override
public void onCreate() {
super.onCreate();
queue= Volley.newRequestQueue(getApplicationContext());
}
public static RequestQueue getHttpQueue(){
return queue;
}
}
2.实现请求:
private void Volley_Post() {
String url="api接口";
StringRequest request=new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
try {
try {
JSONObject object=new JSONObject(s);
String reuslt= object.getString("return");
Toast.makeText(RegisterActivity.this, reuslt, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(s);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String,String> map=new HashMap<String,String>();
map.put("username", etusername.getText().toString());
map.put("password", etpwd.getText().toString());
map.put("email", etemail.getText().toString());
map.put("mobile", etphone.getText().toString());
return map;
}
};
MyApplication.getHttpQueue().add(request); //加入到请求队列
request.setTag("regPost");
}
}