0

我有这个代码,

应该呼叫并等待

异步任务“getCreator”


private String getcreator(String id) {
        String creator = null;
        Log.e("","entro proprio in getcreator "+ id);
        if (id != null) {
            String srt = "";
            getCreator chn = new getCreator();
            chn.execute(id, null, null);
            try {
                srt = chn.get();
                Log.e("","predodo qulacosa da getcreator");
            } catch (Exception e) {
                Log.e("","exceltion in getcreator");
                e.printStackTrace();
            }

            JSONObject jObject = null;
            try {
                jObject = new JSONObject(srt);
                creator = jObject.getString("name");
            } catch (JSONException e) {
                // Json parse error usually
            }
        }
        return creator;
    }

但是 AsyncTask getCreator从不执行它的 doinbackground() !!!


    public class getCreator extends AsyncTask<String, String, String> {
        public String res;

        @Override
        protected void onProgressUpdate(String... progress) {
        }

        @Override
        protected void onPostExecute(String result) {
        }

        @Override
        protected String doInBackground(String... symbol) {
            String result = "";
            Log.e("","entro in getcreator"+ symbol[0]);
            DefaultHttpClient client = new DefaultHttpClient();
            HttpParams httpParameters = new BasicHttpParams();
            // Set the timeout in milliseconds until a connection is
            // established.
            // The default value is zero, that means the timeout is not
            // used.
            int timeoutConnection = 3000;
            HttpConnectionParams.setConnectionTimeout(httpParameters,
                    timeoutConnection);
            // Set the default socket timeout (SO_TIMEOUT)
            // in milliseconds which is the timeout for waiting for data.
            int timeoutSocket = 5000;
            HttpConnectionParams
            .setSoTimeout(httpParameters, timeoutSocket);
            client.setParams(httpParameters);
            String srt = "";
            String url = "http://graph.facebook.com/".concat(symbol[0]);
            HttpGet getMethod = new HttpGet(url);
            try {
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                srt = client.execute(getMethod, responseHandler);

                result = srt;

            } catch (Throwable t) {
                // Toast.makeText(getApplicationContext(),
                // "Internet Problem", Toast.LENGTH_SHORT).show();
            }
            Log.e("","esco in getcreator");
            return result;
        }
    }

请帮忙!!!!


准确地说: doInBackground() 甚至没有被执行....

定期调用 private String getcreator(String id) 方法,并生成其日志。

帮助!!!

4

4 回答 4

1

日志说什么?getcreator(String id) 方法在哪里被调用?

(Cosa leggi nei log?Il metodo getcreator(String id) dove viene richiamato?)


[ - 等待 OP - ]

你搞错了。您不必从 AsyncTask 中“获取”某些东西。

srt = chn.get();

AsycnTask 正在执行一个异步任务,所以你启动它就可以了。“更新”全部在任务上,在 onPostExecute() 中。

(正如评论指出的那样,您可以这样做,但这很“糟糕”)

首先,构造函数中的这三个参数chn.execute(id, null, null)不是最上面的那个AsyncTask<String, String, String>。它们是您在doInBackground(String... symbol). So symbol[0]is id, symbol[1]andsymbol[2]为空

只是为了知识:

Log.e -> ERROR
Log.d -> DEBUG
Log.i -> INFO

你实际上是在“调试”,所以你应该使用 Log.d

于 2013-07-01T17:02:20.593 回答
1

尝试使用new getCreator().execute(id, "", ""); 并在onPostExecute写“ Log.i("","post execute");”后发布日志猫

于 2013-07-01T17:06:58.750 回答
1

调用 的get()方法AsyncTask会阻塞主线程,等待返回结果。这有效地使使用 AsyncTask 成为同步操作,在这种情况下,使用 AsyncTask 毫无意义。

我能想到使用该get()方法的唯一原因是来自主(UI)线程以外的线程,尽管我想不出很多理由这样做。

http://developer.android.com/reference/android/os/AsyncTask.html

您可以在 中返回结果doInbackground。计算结果doInBackground()是 的参数onPostExecute

由于您的 asynctask 是您活动的内部类。您可以声明 String create为您的活动类变量并使用它。

private String getcreator(String id) {
 if(id!=null) // make sure id is not null
 { 
 GetCreator chn = new GetCreator();
 chn.execute(id); 
 // call execute to invoke asynctask
 // there is no need to pass null
 // pass the id to doInBackground()  
 }
}

异步任务

public class GetCreator extends AsyncTask<String, String, String> {
    public String res;

    @Override
    protected void onPostExecute(String result) {
     if(result!=null)
     {
        JSONObject jObject = null;
        try {
            jObject = new JSONObject(result);
            creator = jObject.getString("name");
        } catch (JSONException e) {
            // Json parse error usually
            e.printStacktace(); 
        }
     }  
    }

    @Override
    protected String doInBackground(String... symbol) {
        String result = "";
        Log.e("","entro in getcreator"+ symbol[0]);
        DefaultHttpClient client = new DefaultHttpClient();
        HttpParams httpParameters = new BasicHttpParams();
        // Set the timeout in milliseconds until a connection is
        // established.
        // The default value is zero, that means the timeout is not
        // used.
        int timeoutConnection = 3000;
        HttpConnectionParams.setConnectionTimeout(httpParameters,
                timeoutConnection);
        // Set the default socket timeout (SO_TIMEOUT)
        // in milliseconds which is the timeout for waiting for data.
        int timeoutSocket = 5000;
        HttpConnectionParams
        .setSoTimeout(httpParameters, timeoutSocket);
        client.setParams(httpParameters);
        String srt = "";
        String url = "http://graph.facebook.com/".concat(symbol[0]);
        HttpGet getMethod = new HttpGet(url);
        try {
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            srt = client.execute(getMethod, responseHandler);

            result = srt;

        } catch (Throwable t) {
            // Toast.makeText(getApplicationContext(),
            // "Internet Problem", Toast.LENGTH_SHORT).show();
        }
        Log.e("","esco in getcreator");
        return result; 
    }
}
于 2013-07-01T17:26:42.243 回答
1

首先,你的方法设计getcreator()是错误的。

AsyncTask异步执行,但您的第一个线程(UI 线程?)将等待结果。 AsyncTask在这种情况下是没用的。

您可能应该在您的onPostExecute()方法中更新您的 UI(或对结果做任何您想做的事情) AsyncTask,而不会阻塞第一个线程。

于 2013-07-01T17:38:36.170 回答