先上一下这个类AsyncWeiboRunner.java的注释:
/**
* Encapsulation main Weibo APIs, Include: 1. getRquestToken , 2. getAccessToken, 3. url request.
* Implements a weibo api as a asynchronized way. Every object used this runner should implement interface RequestListener.
*
* @author ZhangJie ([email protected])
*/
可以理解ZhangJie 是想做一个asynchronized的方式访问微薄的服务端(进行发帖等操作),不过TA下面的代码很让人费解,不知道是TA的注释写错了还是代码写错了:
new Thread(){
@Override public void run() {
try {
String resp = mWeibo.request(context, url, params, httpMethod, mWeibo.getAccessToken());
listener.onComplete(resp);
} catch (WeiboException e) {
listener.onError(e);
}
}
}.run();
大家注意这个地方,是调用run方法,那么(public void run() )并没有在新的线程运行,依然在主程线中运行!调用的是Thread的run()方法,这样就相当于调用了一个普通类的方法,导致并没有创建新的线程来运行run()中的代码。
所以到这你应该明白,AsyncWeiboRunner没有异步功能!
我修改了一下,使用AsyncTask来完成异步的功能。详细请看我修改后的代码:
/* * Copyright 2011 Sina. * * Licensed under the Apache License and Weibo License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.open.weibo.com * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.weibo.net; import java.io.IOException; import android.content.Context; import android.os.AsyncTask; /** * Encapsulation main Weibo APIs, Include: 1. getRquestToken , 2. getAccessToken, 3. url request. * Implements a weibo api as a asynchronized way. Every object used this runner should implement interface RequestListener. * * @author ZhangJie ([email protected]) */ public class AsyncWeiboRunner { private Weibo mWeibo; private Context mContext; private String mUrl; private WeiboParameters mParams; private String mHttpMethod; private RequestListener mListener; public AsyncWeiboRunner(Weibo weibo){ this.mWeibo = weibo; } public void request(final Context context, final String url, final WeiboParameters params, final String httpMethod, final RequestListener listener){ // new Thread(){ // @Override public void run() { // try { // String resp = mWeibo.request(context, url, params, httpMethod, mWeibo.getAccessToken()); // listener.onComplete(resp); // } catch (WeiboException e) { // listener.onError(e); // } // } // }.run(); mContext = context; mUrl = url; mParams = params; mHttpMethod = httpMethod; mListener = listener; new WeiboAsyncTask().execute(null); } class WeiboAsyncTask extends AsyncTask<Void, WeiboException, String>{ @Override protected String doInBackground(Void... arg0) { String result = null; try { result = mWeibo.request(mContext, mUrl, mParams, mHttpMethod, mWeibo.getAccessToken()); } catch (WeiboException e) { publishProgress(e); return null; } return result; } @Override protected void onProgressUpdate(WeiboException... values) { mListener.onError(values[0]); } @Override protected void onPostExecute(String result) { if (result != null) mListener.onComplete(result); } } public static interface RequestListener { public void onComplete(String response); public void onIOException(IOException e); public void onError(WeiboException e); } }