[我帮新浪来改错]新浪微博SDK中AsyncWeiboRunner的诡异设计


先上一下这个类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 (zhangjie2@staff.sina.com.cn)
 */

 

可以理解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 (zhangjie2@staff.sina.com.cn)
 */
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);

    }
	
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值