Xutils和Volley请求服务器的使用

public void getSmsCode(final String phoneNumber) {
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("phoneNumber", phoneNumber);
		String httpurl = Constant.httpUrl + "sendSmsCode.do?";
		new HttpUtils().send(HttpMethod.GET, httpurl, params,
				new RequestCallBack() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						Toast.makeText(RegisterDealActivity.this, "获取验证码失败!",
								Toast.LENGTH_SHORT).show();
						dia.cancel();
					}

					@Override
					public void onSuccess(ResponseInfo resultInfo) {
						String result = resultInfo.result;
						Log.i("onResponse", "resultInfo:" + result);
						// -04/Send:0/Consumption:0/Tmoney:0/sid:
						 Toast.makeText(RegisterDealActivity.this,
								 "收到验证码", Toast.LENGTH_SHORT).show();
						dia.cancel();
						
					}
					
				});
		
	}
	下面是volley框架请求网络:
 
  
	public void getSmsCode(final String phoneNumber){
		String httpurl = Constant.httpUrl + "sendSmsCode.do?";
		stringRequest = new StringRequest(Request.Method.POST, httpurl,
				new Response.Listener() {
					@Override
					public void onResponse(String response) {
						Log.i("onResponse", response);
						Toast.makeText(RegisteSubmitActivity.this, "请注意查收验证码",
								Toast.LENGTH_SHORT).show();
						dia.cancel();
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						Toast.makeText(RegisteSubmitActivity.this, "获取验证码失败!",
								Toast.LENGTH_SHORT).show();
						dia.cancel();
					}
				}) {
			@Override
			protected Map getParams() {
				// 在这里设置需要post的参数
				Map map = new HashMap();
				map.put("phoneNumber", phoneNumber);
				return map;
			}
		};
		stringRequest.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
		requestQueue.add(stringRequest);
		
	}


 
  

你可能感兴趣的:(Xutils和Volley请求服务器的使用)