loginUtil

httpManager

</pre><pre code_snippet_id="1564921" snippet_file_name="blog_20160125_3_1142369" name="code" class="java">import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import android.app.ProgressDialog;
import android.content.Context;

import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

// 网络框架封装
public class HttpManager {

	private String url;
	private Map<String, String> params;
	private HttpUtils http;
	private boolean isShowDialog;
	private Context context;

	public HttpManager(Context context, String url, Map<String, String> params,
			OnRequestResonseListener listener, boolean isShowDialog) {
		this.context = context;
		this.url = url;
		this.params = params;
		this.onRequestResonseListener = listener;
		this.isShowDialog = isShowDialog;
		//方式1   xutils
		//  httputils   dbutils   viewutils   bimaputils
		http = new HttpUtils();
		//方式2  volley 能使网络通信更快,更简单,更健壮
		requestQueue = Volley.newRequestQueue(context);
	}

	public void requestByVolley() {
		StringRequest request = new StringRequest(url, new Listener<String>() {

			@Override
			public void onResponse(String arg0) {
				if (onRequestResonseListener != null) {
					onRequestResonseListener.onSucesss(arg0);
				}
			}
		}, new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError arg0) {
				if (onRequestResonseListener != null) {
					onRequestResonseListener.onFailure(arg0.getMessage());
				}
			}
		});
	
		requestQueue.add(request);
		requestQueue.start();
	}

	public HttpManager(Context context, String url, Map<String, String> params,
			OnRequestResonseListener listener) {
		this(context, url, params, listener, true);
	}

	public void get() {
		HttpMethod get = HttpMethod.GET;
		request(get);
	}

	public void post() {
		HttpMethod post = HttpMethod.POST;
		request(post);
	}

	private void request(HttpMethod get) {
		RequestParams requestParams = loadRequestParams();
		http.send(get, url, requestParams, new RequestCallBack<String>() {

			private ProgressDialog dialog;

			@Override
			public void onStart() {
				super.onStart();
				if (isShowDialog)
					dialog = ProgressDialog.show(context, null, "正在拼命加载中..");
			}

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				if (dialog != null)
					dialog.dismiss();
				if (onRequestResonseListener != null)
					onRequestResonseListener.onSucesss(responseInfo.result);

			}

			@Override
			public void onFailure(HttpException error, String msg) {
				if (dialog != null)
					dialog.dismiss();
				if (onRequestResonseListener != null)
					onRequestResonseListener.onFailure(msg);
			}
		});
	}

	private RequestParams loadRequestParams() {
		if (this.params == null) {
			return null;
		}
		RequestParams requestParams = new RequestParams();
		Set<Entry<String, String>> entrySet = this.params.entrySet();
		for (Entry<String, String> entry : entrySet) {
			String key = entry.getKey();
			String value = entry.getValue();
			requestParams.addBodyParameter(key, value);
		}
		return requestParams;
	}

	private OnRequestResonseListener onRequestResonseListener;
	private RequestQueue requestQueue;

	public OnRequestResonseListener getOnRequestResonseListener() {
		return onRequestResonseListener;
	}

	public void setOnRequestResonseListener(
			OnRequestResonseListener onRequestResonseListener) {
		this.onRequestResonseListener = onRequestResonseListener;
	}

	public interface OnRequestResonseListener {
		void onSucesss(String json);

		void onFailure(String errorMsg);
	}
}


LoginActivity

</pre><pre name="code" class="java">
public class LoginActivity extends Activity {

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

		ViewUtils.inject(this);
	}

	@OnClick(R.id.btn_login)
	public void login(View view) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("username","itcastz10");
		params.put("password","admin");
		HttpManager http = new HttpManager(this, Constance.LOGINURL, params,
				new OnRequestResonseListener() {

					@Override
					public void onSucesss(String json) {
						if (checkJson(json)) {
							Toast.makeText(getApplicationContext(), "登录成功",
									Toast.LENGTH_SHORT).show();
						}

					}

					@Override
					public void onFailure(String errorMsg) {
						Toast.makeText(getApplicationContext(), "登录失败",
								Toast.LENGTH_SHORT).show();

					}
				});
		http.post();
	}

	protected boolean checkJson(String json) {
		try {
			JSONObject rootObj = new JSONObject(json);
			String responseVal = rootObj.getString("response");
			if (TextUtils.equals("error", responseVal)) {
				return false;
			} else {
				return true;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return false;
	}
}

你可能感兴趣的:(loginUtil)