0

私はこのコードを持っています、

CALL および WAIT することになっている

AsyncTask "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() のタスクで行われます。

(コメントが指摘したように、これは可能ですが、「悪い」です)

まず第一に、コンストラクターのこれら 3 つのパラメーターはchn.execute(id, null, null)top のものではありませんAsyncTask<String, String, String>。で取得するパラメータですdoInBackground(String... symbol)。そうsymbol[0]ですidsymbol[1]およびsymbol[2]は null です

そして、知識のために:

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

実際に「デバッグ」しているので、Log.d を使用する必要があります。

于 2013-07-01T17:02:20.593 に答える
1

まず、メソッドの設計getcreator()が間違っています。

AsyncTask非同期で実行されますが、最初のスレッド (UI スレッド?) は結果を待っています。 AsyncTaskこの場合は役に立ちません。

おそらく、最初のスレッドをブロックせずに、 のonPostExecute()メソッドでUI を更新する (または結果に対して必要なことを行う) 必要があります。AsyncTask

于 2013-07-01T17:38:36.170 に答える
1

get()のメソッドを呼び出すAsyncTaskと、メイン スレッドがブロックされ、結果が返されるのを待ちます。これにより、実質的に AsyncTask の使用が同期操作になり、その場合、AsyncTask を使用しても意味がありません。

このメソッドを使用する唯一の理由はget()、メイン (UI) スレッド以外のスレッドからのものだと思いますが、そうする理由はあまり思いつきません。

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

で結果を返すことができますdoInbackgrounddoInBackground()演算結果はへのパラメータ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 に答える