publicclassMyAsyncTaskextendsAsyncTask {
Stringresult;
@Override
protectedvoidonPreExecute() {
super.onPreExecute();
}
@Override
protectedString doInBackground(Object... params) {
String url = (String) params[0];
Map map = (Map) params[1];
InputStream is =null;
HttpClient client =newDefaultHttpClient();
client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,20000);
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,20000);
HttpPost post =newHttpPost(url);
List list =newArrayList();
Set set = map.keySet();
for(String key : set) {
list.add(newBasicNameValuePair(key, map.get(key).toString()));
}
try{
post.setEntity(newUrlEncodedFormEntity(list));
HttpResponse response = client.execute(post);
if(response.getStatusLine().getStatusCode() ==200) {
is = response.getEntity().getContent();
ByteArrayOutputStream bos =newByteArrayOutputStream();
byte[] b =newbyte[1024];
intlen =0;
while((len = is.read(b)) != -1) {
bos.write(b,0, len);
}
result= bos.toString("UTF-8");
}
}catch(UnsupportedEncodingException e) {
e.printStackTrace();
}catch(ClientProtocolException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally{
if(is !=null) {
try{
is.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
returnresult;
}
@Override
protectedvoidonPostExecute(String s) {
super.onPostExecute(s);
}
}
网友评论