先上一下這個類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);
}
}