2015年7月13日
package com.zhihe.xqsh.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.params.ConnRouteParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
import com.zhihe.xqsh.network.ServerErrorException;
import android.accounts.NetworkErrorException;
import android.annotation.SuppressLint;
import android.util.Log;
public class CustomerHttpClient {
private static final String TAG = CustomerHttpClient.class.getSimpleName();
private static DefaultHttpClient customerHttpClient;
private CustomerHttpClient() {
}
public static synchronized HttpClient getHttpClient() {
if (null == customerHttpClient) {
HttpParams params = new BasicHttpParams();
// 设置�?��基本参数
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUseExpectContinue(params, true);
HttpProtocolParams.setUserAgent(params, "Mozilla/5.0(Linux;U;Android 2.2.1;en-us;Nexus One Build.FRG83) "
+ "AppleWebKit/553.1(KHTML,like Gecko) Version/4.0 Mobile Safari/533.1");
// 超时设置
/* 从连接池中取连接的超时时�?*/
ConnManagerParams.setTimeout(params, 2000);
ConnManagerParams.setMaxTotalConnections(params, 800);
/* 连接超时 */
HttpConnectionParams.setConnectionTimeout(params, 5000);
/* 请求超时 */
HttpConnectionParams.setSoTimeout(params, 10000);
// 设置我们的HttpClient支持HTTP和HTTPS两种模式
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
// 使用线程安全的连接管理来创建HttpClient
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
// �?��连接数:ConnManagerParams.setMaxTotalConnections(params, 50);
customerHttpClient = new DefaultHttpClient(conMgr, params);
}
return customerHttpClient;
}
/**
* 以get方式提交数据
*
* @param url 提交地址
* @param params 参数
* @return 响应结果
* @throws ServerErrorException 请求失败
* @throws NetworkErrorException 连接失败
*/
public static String get(String url, String params) throws ServerErrorException, NetworkErrorException {
int tryTimes = 0;
NullPointerException ex;
do {
try {
return tryGet(url, params);
} catch (NullPointerException e) {
ex = e;
tryTimes++;
}
} while (tryTimes < 3);
throw ex;
}
/**
* 以get方式提交数据
*
* @param url 提交地址
* @param params 参数
* @return 响应结果
* @throws ServerErrorException 请求失败
* @throws NetworkErrorException 连接失败
*/
public static String tryGet(String url, String params) throws ServerErrorException, NetworkErrorException {
try {
HttpGet request = new HttpGet(url + params);
/*if (LotteryApplication.isCmwap()) {
org.apache.http.HttpHost proxy = new org.apache.http.HttpHost("10.0.0.172", 80, "http");
HttpParams httpParams = new BasicHttpParams();
ConnRouteParams.setDefaultProxy(httpParams, proxy);
request.setParams(httpParams);
}*/
HttpClient client = getHttpClient();
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
throw new ServerErrorException("��������æ�����Ժ�����");
}
HttpEntity resEntity = response.getEntity();
String result = (resEntity == null) ? null : EntityUtils.toString(resEntity, "UTF-8");
return result;
} catch (UnsupportedEncodingException e) {
logw(e.getMessage());
return null;
} catch (ClientProtocolException e) {
logw(e.getMessage());
return null;
} catch (IOException e) {
throw new NetworkErrorException("���Ӳ��ɹ���������������", e);
}
}
private static void logw(String string) {
if (string != null) {
Log.w(TAG, string);
}
}
/**
* 以post方式提交数据
*
* @param url 提交地址
* @param params 参数
* @return 响应结果
* @throws ServerErrorException 请求失败
* @throws NetworkErrorException 连接失败
*/
public static String post(String url, List<NameValuePair> params) throws ServerErrorException, NetworkErrorException {
return post(url, params, null);
}
/**
* 以post方式提交数据
*
* @param url 提交地址
* @param params 参数
* @param soTimeout 响应超时时间,单位毫�?
* @return 响应结果
* @throws ServerErrorException 请求失败
* @throws NetworkErrorException 连接失败
*/
public static String post(String url, List<NameValuePair> params, int soTimeout) throws ServerErrorException,
NetworkErrorException {
HttpParams httpParams;
if (soTimeout <= 0) {
httpParams = null;
} else {
httpParams = new BasicHttpParams();
HttpConnectionParams.setSoTimeout(httpParams, soTimeout);
}
return post(url, params, httpParams);
}
/**
* 以post方式提交数据
*
* @param url 提交地址
* @param params 参数
* @param httpParams http参数
* @return 响应结果
* @throws ServerErrorException 请求失败
* @throws NetworkErrorException 连接失败
*/
public static String post(String url, List<NameValuePair> params, HttpParams httpParams) throws ServerErrorException,
NetworkErrorException {
int tryTimes = 0;
NullPointerException ex;
do {
try {
return tryPost(url, params, httpParams);
} catch (NullPointerException e) {
ex = e;
tryTimes++;
}
} while (tryTimes < 3);
throw ex;
}
/**
* 以post方式提交数据
*
* @param url 提交地址
* @param params 参数
* @param httpParams http参数
* @return 响应结果
* @throws ServerErrorException 请求失败
* @throws NetworkErrorException 连接失败
*/
public static String tryPost(String url, List<NameValuePair> params, HttpParams httpParams) throws ServerErrorException,
NetworkErrorException {
try {
HttpPost request = new HttpPost(url);
if (params != null && params.size() > 0) {
request.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
}
// if (LotteryApplication.isCmwap()) {
// org.apache.http.HttpHost proxy = new org.apache.http.HttpHost("10.0.0.172", 80, "http");
// if (httpParams == null)
// httpParams = new BasicHttpParams();
// ConnRouteParams.setDefaultProxy(httpParams, proxy);
// }
if (httpParams != null)
request.setParams(httpParams);
//Log.v("CS", params.toString());
HttpClient client = getHttpClient();
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
//Log.v("CS", params.toString());
//Log.v("CS", response.getStatusLine().getStatusCode() + "");
request.abort();
throw new ServerErrorException("��������æ�����Ժ�����");
}
if (response.getStatusLine ().getStatusCode () != 200) {
request.abort(); //�ж�����,���������Կ�ʼ��һ������
return null;
}
HttpEntity resEntity = response.getEntity();
String result = (resEntity == null) ? null : EntityUtils.toString(resEntity, "UTF-8");
//Log.v("CS", params.toString() + "||||" + result);
return result;
} catch (UnsupportedEncodingException e) {
logw(e.getMessage());
return null;
} catch (ClientProtocolException e) {
logw(e.getMessage());
return null;
} catch (IOException e) {
throw new NetworkErrorException(e.getMessage(), e);
//throw new NetworkErrorException("连接不成功,请检查网络设�?, e);
}
}
@SuppressLint("SdCardPath")
public static String download(String url) throws ServerErrorException, NetworkErrorException {
try {
//Log.i("http-download", url);
HttpPost request = new HttpPost(url);
HttpClient client = getHttpClient();
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
throw new ServerErrorException("��������æ�����Ժ�����");
}
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
if (is == null)
throw new ServerErrorException("stream is null ");
String fileExt = url.substring(url.lastIndexOf(".") + 1, url.length()).toLowerCase();
String fileName = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));
File tempFile = new File("/sdcard/" + fileName + "." + fileExt);
if (!tempFile.exists())
tempFile.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
byte[] buf = new byte[1024];
int ch;
while ((ch = is.read(buf)) != -1) {
fileOutputStream.write(buf, 0, ch);
}
fileOutputStream.flush();
fileOutputStream.close();
return tempFile.getAbsolutePath();
} catch (UnsupportedEncodingException e) {
logw(e.getMessage());
return null;
} catch (ClientProtocolException e) {
logw(e.getMessage());
return null;
} catch (IOException e) {
throw new NetworkErrorException(e.getMessage(), e);
}
}
/**
* 清空cookie
*/
public static void clearCookie() {
if (customerHttpClient != null)
customerHttpClient.getCookieStore().clear();
}
/**
* 清除指定cookie
*
* @param name cookie名称
*/
public static void clearCookie(String name) {
if (customerHttpClient == null)
return;
BasicClientCookie expiredCookie = new BasicClientCookie(name, "null");
expiredCookie.setExpiryDate(new Date(System.currentTimeMillis() - 1000));
customerHttpClient.getCookieStore().addCookie(expiredCookie);
}
}
posted @
2015-07-13 22:10 Terry Zou 阅读(267) |
评论 (0) |
编辑 收藏
http://yunpan.cn/ccdbTgQaYa4U7
posted @
2015-07-13 11:04 Terry Zou 阅读(135) |
评论 (0) |
编辑 收藏
2015年7月8日
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical" >
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView_routePlanActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/common_title_back"
android:gravity="center"
android:padding="5dp" >
<Button
android:id="@+id/button_transit_routePlan"
android:layout_width="0dp"
android:drawableLeft="@drawable/ic_bus"
android:padding="5dp"
android:background="@drawable/selector_white_gray"
android:layout_height="50dp"
android:layout_weight="1.0"
android:onClick="SearchButtonProcess"
android:text="公交" />
<Button
android:id="@+id/button_drive_routePlan"
android:layout_width="0dp"
android:drawableLeft="@drawable/ic_drive"
android:layout_height="50dp"
android:layout_weight="1.0"
android:padding="5dp"
android:background="@drawable/selector_white_gray"
android:onClick="SearchButtonProcess"
android:text="驾车" />
<Button
android:id="@+id/button_walk_routePlan"
android:layout_width="0dp"
android:layout_height="50dp"
android:drawableLeft="@drawable/ic_walk"
android:layout_weight="1.0"
android:padding="5dp"
android:background="@drawable/selector_white_gray"
android:onClick="SearchButtonProcess"
android:text="步行" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout_node_routePlan"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dip"
android:visibility="gone"
android:gravity="bottom|center_horizontal" >
<Button
android:id="@+id/button_pre_routePlan"
android:layout_width="60dp"
android:layout_height="30dp"
android:layout_marginRight="2dip"
android:background="@drawable/pre_"
android:onClick="nodeClick" />
<Button
android:id="@+id/button_next_routePlan"
android:layout_width="60dp"
android:layout_height="30dp"
android:layout_marginLeft="2dip"
android:background="@drawable/next_"
android:onClick="nodeClick" />
</LinearLayout>
</FrameLayout>
posted @
2015-07-08 23:57 Terry Zou|
编辑 收藏
<LinearLayout
android:id="@+id/estate_linear"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:layout_weight="1"
android:background="@drawable/border_rounded_gray_white"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:layout_margin="5dp"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/object_btn_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_gravity="center_vertical|right"
android:background="@drawable/btn_search"
android:contentDescription="@null"
android:scaleType="fitXY" />
<RelativeLayout
android:layout_width="1dp"
android:layout_height="33dp"
android:layout_marginLeft="8dp"
android:background="@color/color_line" />
<EditText
android:id="@+id/object_et_content"
style="@style/StringSearchText"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="2dp"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:background="@null"
android:hint="@string/tip_search_hint"
android:imeOptions="actionSend"
android:singleLine="true"
android:textCursorDrawable="@null"
android:textColorHint="#626463" />
<ImageButton
android:id="@+id/object_btn_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="10dp"
android:background="@drawable/ic_clear" />
</LinearLayout>
border_rounded_gray_white.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 连框颜色值 -->
<item>
<shape>
<solid android:color="@color/bg_gray" />
<corners
android:bottomLeftRadius="3dp"
android:bottomRightRadius="3dp"
android:topLeftRadius="3dp"
android:topRightRadius="3dp" />
</shape>
</item>
<!-- 主体背景颜色值 -->
<item
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp">
<shape>
<solid android:color="@color/white" />
<corners
android:bottomLeftRadius="3dp"
android:bottomRightRadius="3dp"
android:topLeftRadius="3dp"
android:topRightRadius="3dp" />
</shape>
</item>
</layer-list>
<style name="StringSearchText">
<item name="android:textSize">14dp</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">@android:color/black</item>
</style>
btn_search.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 没有焦点时的背景图片 -->
<item android:drawable="@drawable/ic_search_normal" android:state_window_focused="false"/>
<!-- 非触摸模式下获得焦点并单击时的背景图片 -->
<item android:drawable="@drawable/ic_search_pressed" android:state_focused="true" android:state_pressed="true"/>
<!-- 触摸模式下单击时的背景图片 -->
<item android:drawable="@drawable/ic_search_pressed" android:state_focused="false" android:state_pressed="true"/>
<!-- 选中时的图片背景 -->
<item android:drawable="@drawable/ic_search_pressed" android:state_selected="true"/>
<!-- 获得焦点时的图片背景 -->
<item android:drawable="@drawable/ic_search_pressed" android:state_focused="true"/>
<!-- 默认图片背景 -->
<item android:drawable="@drawable/ic_search_normal"/>
</selector>
<color name="color_line">#bebebe</color>
private TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void afterTextChanged(Editable s) {
mKeywords = tv_keyword.getText().toString();
AgUtils.log(TAG+"mKeywords:"+mKeywords, 4);
if (TextUtils.isEmpty(mKeywords)) {
object_btn_del.setVisibility(View.GONE);
} else {
object_btn_del.setVisibility(View.VISIBLE);
searchIndexListInfo();
}
}
};
tv_keyword.addTextChangedListener(textWatcher);
posted @
2015-07-08 23:55 Terry Zou|
编辑 收藏