AsyncTask

作者: 彼岸婲已落 | 来源:发表于2017-04-17 09:01 被阅读0次

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);

}

}

相关文章

网友评论

      本文标题:AsyncTask

      本文链接:https://www.haomeiwen.com/subject/ylnzattx.html