从片段调用时,Android异步任务阻止

时间:2014-02-12 23:32:39

标签: android google-app-engine android-asynctask

我试图制作我认为对谷歌应用引擎项目的简单调用。 从单元测试中,如果我直接访问api,则调用正常。但是我不能这样做,因为调用是阻塞的,所以它必须从异步任务运行。

似乎发生的事情是: 继承自ActivityInstrumentationTestCase2的单元测试会设置活动并填充一些值

然后我点击按钮发出请求:

   final Button mBet = (Button) mActivity.findViewById(R.id.mybutton);
   mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mName.setText("blah");
                mBet.performClick();
            }
        });

然后调用底层代码启动异步任务:

public void bet() throws IOException, InterruptedException {

    mMakeTask = new AsyncTask<Void, Void, Model>() {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mBetWait = new ProgressDialog(getActivity());
            mBetWait.setCancelable(true);
            mBetWait.show();
        }

        @Override
        protected Model doInBackground(Void... voids) {
            try {
                mModel = mController.makeBet(mModel.getBetName(), mModel.getDescription(), mModel.getAnswer(), mModel.getAgainst());
            } catch (IOException e) {
                e.printStackTrace();
            }

            return mModel;
        }

        @Override
        protected void onPostExecute(BetModel model) {
            super.onPostExecute(model);
            Context context = getActivity();
            Intent intent = new Intent(context, OtherActivity.class);
            intent.putExtra("bet", mModel);
            mBetWait.dismiss();

            context.startActivity(intent);
        }
    };


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        mMakeTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    } else {
        mMakeTask.execute();
    }
}

我忘了添加...当调用控制器时,它归结为一个appengine调用:

public BetInfo makeBet(BetRequest info) throws IOException {
    Betrequestendpoint.Builder endpointBuilder = new Betrequestendpoint.Builder(
            AndroidHttp.newCompatibleTransport(),
            new JacksonFactory(),
            new HttpRequestInitializer() {
                public void initialize(HttpRequest httpRequest) { }
            });
    Betrequestendpoint endpoint = CloudEndpointUtils.updateBuilder(
            endpointBuilder).build();

    BetRequest result = endpoint.insertBetRequest(info).execute();
    BetInfo resultInfo = new BetInfo();
    resultInfo.setName(result.getName());
    resultInfo.setDescription(result.getDescription());
    resultInfo.setId(result.getId());

    return resultInfo;
}

为了使这更奇怪......请求调用似乎被阻止从具有堆栈跟踪的客户端:

"<13> AsyncTask #2"@831,912,607,944 in group "main": RUNNING
toUpperCase():3266, Character {java.lang}
toUpperCase():3251, Character {java.lang}
toUpperCase():162, CaseMapper {java.lang}
toUpperCase():1548, String {java.lang}
initServiceInfo():152, Services {org.apache.harmony.security.fortress}
getCacheVersion():211, Services {org.apache.harmony.security.fortress}
getInstance():137, Engine {org.apache.harmony.security.fortress}
getInstance():77, KeyManagerFactory {javax.net.ssl}
createDefaultKeyManager():362, SSLParametersImpl {com.android.org.conscrypt}
getDefaultKeyManager():355, SSLParametersImpl {com.android.org.conscrypt}
<init>():111, SSLParametersImpl {com.android.org.conscrypt}
getDefault():146, SSLParametersImpl {com.android.org.conscrypt}
<init>():34, OpenSSLSocketFactoryImpl {com.android.org.conscrypt}
newInstanceImpl():-1, Class {java.lang}
newInstance():1208, Class {java.lang}
getDefault():56, SSLSocketFactory {javax.net.ssl}
<clinit>():114, HttpsURLConnection$NoPreloadHolder {javax.net.ssl}
getDefaultSSLSocketFactory():163, HttpsURLConnection {javax.net.ssl}
copyWithDefaults():363, OkHttpClient {com.android.okhttp}
open():345, OkHttpClient {com.android.okhttp}
open():340, OkHttpClient {com.android.okhttp}
openConnection():28, HttpHandler {com.android.okhttp}
openConnection():479, URL {java.net}
buildRequest():133, NetHttpTransport {com.google.api.client.http.javanet}
buildRequest():68, NetHttpTransport {com.google.api.client.http.javanet}
execute():858, HttpRequest {com.google.api.client.http}
executeUnparsed():410, AbstractGoogleClientRequest {com.google.api.client.googleapis.services}
executeUnparsed():343, AbstractGoogleClientRequest {com.google.api.client.googleapis.services}
execute():460, AbstractGoogleClientRequest {com.google.api.client.googleapis.services}
makeBet():36, BetController {com.chillypixel.youwereright.controller}
makeBet():36, BetApiController {com.chillypixel.youwereright.controller}
doInBackground():129, BetFragment$1 {com.chillypixel.youwereright.bet}
doInBackground():117, BetFragment$1 {com.chillypixel.youwereright.bet}
call():288, AsyncTask$2 {android.os}
run():237, FutureTask {java.util.concurrent}
runWorker():1112, ThreadPoolExecutor {java.util.concurrent}
run():587, ThreadPoolExecutor$Worker {java.util.concurrent}
run():841, Thread {java.lang}

似乎发生了什么,是我运行测试,它会进入弹出进度对话框的位置。 如果我然后手动与模拟器中的应用程序交互(并回击或某事),则进度对话框消失并且调用完成 - 然后测试通过。

如果我只是不管它,似乎进度对话框很乐意永远旋转。

非常感谢任何帮助。

0 个答案:

没有答案