UI线程中调用
HttpTask httpTask = new HttpTask("http://192.168.1.103:8080/pprice/test");
httpTask.setTaskHandler(new HttpTaskHandler() {
@Override
public void taskSuccessful(String json) {
mTextView.setText(json);
}
@Override
public void taskFailed(String json) {
mTextView.setText(json);
}
});
httpTask.execute("");
*************************************************************************************
继承实现异步类
public class HttpTask extends AsyncTask<String, Integer, String> {
private static final String TAG = "HTTP_TASK";
private String url;
private String method;
private Map<String, String> paramsMap;
public HttpTask(String url) {
super();
this.url = url;
method="get";
}
public HttpTask(String url, String method, Map<String, String> paramsMap) {
super();
this.url = url;
this.method = method;
this.paramsMap = paramsMap;
}
public HttpTask(String url, String method, Map<String, String> paramsMap,
HttpTaskHandler taskHandler) {
super();
this.url = url;
this.method = method;
this.paramsMap = paramsMap;
this.taskHandler = taskHandler;
}
@Override
protected String doInBackground(String... params) {
String str=params[0];
try {
if (method == "post" || method == "POST") {
String json = NetworkTool.executePost(url, paramsMap);
return json;
} else {
String json = NetworkTool.executeGet(url, paramsMap);
return json;
}
} catch (Exception e) {
Log.e(TAG, e.toString());
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(String json) {
if (json != null && json != "") {
Log.d(TAG, "taskSuccessful");
taskHandler.taskSuccessful(json);
} else {
Log.d(TAG, "taskFailed");
taskHandler.taskFailed(json);
}
}
public static interface HttpTaskHandler {
void taskSuccessful(String json);
void taskFailed(String json);
}
HttpTaskHandler taskHandler;
public void setTaskHandler(HttpTaskHandler taskHandler) {
this.taskHandler = taskHandler;
}
}
***********************************************************************************
NetWorkTool类
public class NetworkTool {
private static HttpParams httpParams;
private static HttpClient client;
static {
httpParams = new BasicHttpParams();
//设置连接超时时间
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
//设置请求超时时间
HttpConnectionParams.setSoTimeout(httpParams, 8000);
//建立连接对象
client = new DefaultHttpClient(httpParams);
}
public static String Get(String url,Map<String, String> params) {
if (url == null) {
return "error! url is null";
}
//结果返回字符串
String result = null;
BufferedReader reader = null;
//请求url
StringBuilder resBuilder = new StringBuilder(url);
try {
HttpGet request = new HttpGet();
if ( params==null || params.size()==0) {
request.setURI(new URI(url));
} else {
//请求url拼接
Iterator<String> istr = params.keySet().iterator();
resBuilder.append("?");
String key;
//one
key=istr.next();
resBuilder.append(key);
resBuilder.append("=");
resBuilder.append((String)params.get(key));
//has more
while (istr.hasNext()) {
resBuilder.append("&");
key=istr.next();
resBuilder.append(key);
resBuilder.append("=");
resBuilder.append((String)params.get(key));
}
request.setURI(new URI(resBuilder.toString()));
}
HttpResponse response = client.execute(request);
//开始读取信息
reader = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
StringBuffer strBuffer = new StringBuffer("");
String line = null;
while ((line = reader.readLine()) != null) {
strBuffer.append(line);
}
result = strBuffer.toString();
} catch (Exception e) {
e.printStackTrace();
result = e.getMessage();
} finally {
if (reader != null) {
try {
reader.close();
reader = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
public static String Post(String url,Map<String, String> params) {
if (url == null || params == null) {
return "error! url or params is null";
}
String result = null;
BufferedReader reader = null;
try {
HttpPost request = new HttpPost();
if (params==null || params.size()==0) {
request.setURI(new URI(url));
} else {
request.setURI(new URI(url));
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
Iterator<String> istr = params.keySet().iterator();
String key;
while (istr.hasNext()) {
key=istr.next();
postParameters.add(new BasicNameValuePair(key, (String) params.get(key)));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
postParameters);
request.setEntity(formEntity);
}
HttpResponse response = client.execute(request);
reader = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
StringBuffer strBuffer = new StringBuffer("");
String line = null;
while ((line = reader.readLine()) != null) {
strBuffer.append(line);
}
result = strBuffer.toString();
} catch (Exception e) {
e.printStackTrace();
//返回异常信息
result = e.getMessage();
} finally {
if (reader != null) {
try {
reader.close();
reader = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
}