dasdas

package com.jleo.collectme.util;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.jleo.collectme.android.Request;

public class HttpClientUtil {
	public interface Handler {
		void onSucceed(InputStream is);

		void onFailed();
	}

	public void get(Request request, Handler handler) {
		try {
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost(request.getUrl());
			List<NameValuePair> postParams = new ArrayList<NameValuePair>();
			Set<String> keys = request.getParams().keySet();
			for (String key : keys) {
				postParams.add(new BasicNameValuePair(key, request.getParams()
						.get(key)));
			}

			UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
					postParams);
			post.setEntity(formEntity);
			HttpResponse response = client.execute(post);
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode >= 200 && statusCode <= 299) {
				handler.onSucceed(response.getEntity().getContent());
			} else {
				handler.onFailed();
			}
		} catch (Exception e) {
			handler.onFailed();
		}
	}
}

你可能感兴趣的:(java,apache,android)