import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
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;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import com.lidroid.xutils.BitmapUtils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
public final class EHttpUtil {
private static final int CONNECT_TIMEOUT = 5000;
private static final int READ_TIMEOUT = 5000;
public static final String USER_AGENT = "ting_4.1.7(" + Build.MODEL + "," + Build.VERSION.SDK_INT + ")";
public EHttpUtil() {
throw new IllegalStateException("the bitmapUtils Object is an static Object,you don't need to new! ");
}
public static BitmapUtils bitmapUtils;
public interface CallBack {
void onSuccess(ResponseInfo<String> responseInfo);
void onFailure(HttpException error, String msg);
}
public static BitmapUtils newInstance(Context context, String filePath, int cacheSize) {
return getBitmapUtils(context, filePath, cacheSize);
}
public static BitmapUtils getBitmapUtils(Context context, String filePath, int cacheSize) {
if (filePath == null || cacheSize == 0) {
bitmapUtils = new BitmapUtils(context);
} else {
bitmapUtils = new BitmapUtils(context, filePath, cacheSize);
}
bitmapUtils.configDefaultBitmapMaxSize(512, 256);
bitmapUtils.configDefaultConnectTimeout(CONNECT_TIMEOUT);
bitmapUtils.configDiskCacheEnabled(true);
return bitmapUtils;
}
public static void getStringUseGET(String url, final CallBack callBack) {
RequestParams params = new RequestParams();
params.addHeader("Accept-Encoding", "gzip");
params.addHeader("User-Agent", USER_AGENT);
HttpUtils httpUtils;
httpUtils = new HttpUtils();
httpUtils.send(
HttpRequest.HttpMethod.GET,
url,
params,
new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
callBack.onSuccess(responseInfo);
}
@Override
public void onFailure(HttpException error, String msg) {
callBack.onFailure(error, msg);
}
});
}
public static void downloadFile(final Context context, String dPath, String fileName) {
HttpUtils httpUtils = new HttpUtils();
String mPath = getFileDownloadPath(context);
if (mPath != null) {
HttpHandler handler = httpUtils.download(
dPath,
mPath + "/" + fileName,
true,
true,
new RequestCallBack<File>() {
@Override
public void onStart() {
super.onStart();
T.showShort(context, "download is running in background");
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
super.onLoading(total, current, isUploading);
}
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
L.d("the download is success :" + responseInfo.result.getAbsolutePath());
T.showShort(context, "download success !");
}
@Override
public void onFailure(HttpException error, String msg) {
L.d("the download is failure for this reasion :" + msg);
}
@Override
public void onCancelled() {
super.onCancelled();
}
});
}else {
throw new NullPointerException("the phone path is null,please check your phone!");
}
}
public static String getFileDownloadPath(Context context) {
String ret ;
if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) {
ret = Environment.getExternalStorageDirectory().getAbsolutePath();
} else {
ret = context.getFilesDir().getAbsolutePath();
}
return ret;
}
public static String submitPostData(String strUrlPath,Map<String, String> params, String encode) {
byte[] data = getRequestData(params, encode).toString().getBytes();
try {
URL url = new URL(strUrlPath);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length));
OutputStream outputStream = httpURLConnection.getOutputStream();
outputStream.write(data);
int response = httpURLConnection.getResponseCode();
if(response == HttpURLConnection.HTTP_OK) {
InputStream inptStream = httpURLConnection.getInputStream();
return dealResponseResult(inptStream);
}
} catch (IOException e) {
return "err: " + e.getMessage().toString();
}
return "-1";
}
public static StringBuffer getRequestData(Map<String, String> params, String encode) {
StringBuffer stringBuffer = new StringBuffer();
try {
for(Map.Entry<String, String> entry : params.entrySet()) {
stringBuffer.append(entry.getKey())
.append("=")
.append(URLEncoder.encode(entry.getValue(), encode))
.append("&");
}
stringBuffer.deleteCharAt(stringBuffer.length() - 1);
} catch (Exception e) {
e.printStackTrace();
}
return stringBuffer;
}
public static String dealResponseResult(InputStream inputStream) {
String resultData ;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len;
try {
while((len = inputStream.read(data)) != -1) {
byteArrayOutputStream.write(data, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
resultData = new String(byteArrayOutputStream.toByteArray());
return resultData;
}
}