1

私はアンドロイド(3日前)から始めていますが、私が望むものの解決策を得ることができません。だから、私はここで asyncTask について多くのスレッドを読みましたが、今では混乱していると確信しています。

まず第一に、これは私の最初の質問なので、少なくともこれが正しいことを願っています.

私が欲しいのは、いくつかのサーバーに接続するためのクラスと、その結果です。その後、json または xml を分析します。

これが私がしたことです。これは私の活動クラスです(メインのものから呼び出されます)

    public class LogIn extends Activity implements OnClickListener {

    Button btn =null;

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    btn = (Button) findViewById(R.id.btn_login);
    btn.setOnClickListener( this);
}   

public void onClick(View view) {
    HttpResponse response;
    Intent data = new Intent();
    //---get the EditText view---
    EditText txt_user = (EditText) findViewById(R.id.et_un);
    EditText txt_pwd =  (EditText) findViewById(R.id.et_pw);

    // aca tengo q llamar al conectar y chequear en BD
    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("usr", txt_user.getText().toString()));
    postParameters.add(new BasicNameValuePair("pass", txt_pwd.getText().toString()));
    String URL="whatever";
    try {
        response= new ConectServer(URL, postParameters).execute().get();

        LeerAutentificacionXml l= new LeerAutentificacionXml(response);
        String s=l.Transformar();
        data.setData(Uri.parse(s.toString()));
        setResult(RESULT_OK, data);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //---set the data to pass back---
//  data.setData(Uri.parse(txt_user.getText().toString()+ " " + Uri.parse(txt_pwd.getText().toString())));
//  setResult(RESULT_OK, data);
    //---closes the activity---
    finish();
} }

これは、Web サービスに接続する私のクラスです。

    public class ConectServer extends AsyncTask <Void, Void, HttpResponse> {

private String URL=null;
private ArrayList<NameValuePair> postParameters=null;
/** Single instance of our HttpClient */
private HttpClient mHttpClient;
/** The time it takes for our client to timeout */
public static final int HTTP_TIMEOUT = 30 * 1000;


  public ConectServer(String url, ArrayList<NameValuePair> p) {
      this.URL=url;
      this.postParameters=p;
  }

 private HttpClient getHttpClient() {
     if (mHttpClient == null) {
         mHttpClient = new DefaultHttpClient();
         final HttpParams params = mHttpClient.getParams();
         HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
         HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
         ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
     }
     return mHttpClient;
 }

 /**
 * Performs an HTTP Post request to the specified url with the
 * specified parameters.
 *
 * @param url The web address to post the request to
 * @param postParameters The parameters to send via the request
 * @return The result of the request
 * @throws Exception
 */
@Override
protected HttpResponse doInBackground(Void... params) {
    // TODO Auto-generated method stub
     HttpResponse response = null;
     try {
         HttpClient client = getHttpClient();
         HttpPost request = new HttpPost(this.URL);
         UrlEncodedFormEntity formEntity;
         formEntity = new UrlEncodedFormEntity(this.postParameters);
         request.setEntity(formEntity);
         response = client.execute(request);
     } catch (UnsupportedEncodingException e) {
         e.printStackTrace();
     } catch (ClientProtocolException e) {
        e.printStackTrace();
     } catch (IOException e) {
        e.printStackTrace();
    }

    return response;
}


public void onPreExecute() {
     super.onPreExecute();

}

protected void onPostExecute(HttpResponse response) {
     super.onPostExecute(response);
}}

リスナーの設計パターンについて読みましたが、まず、これが機能しない理由をよりよく理解したいと思います。サーバーからエラーが発生しました。これが正しいかどうか、または大きな newby の失敗が起こっているかどうかを知りたいです。

事前にt​​hx。

4

2 に答える 2

2

呼び出しは が終了asyncTask.get()するのを待ってAsyncTaskから、結果を返します。それは基本的にの目的を無効にしますAsyncTask-全体のポイントAsyncTaskは、長時間実行されるタスクがバックグラウンドスレッドで実行され、この間UIスレッドがブロックされないことです。呼び出す.get()と、バックグラウンド スレッドが終了するのを待っている UI スレッドがブロックされます。

get()そのため、結果が に利用可能になったときに発生する必要があるすべてのアクションを使用して移動しないでくださいonPostExecute(..)。このようなもの:

protected void onPostExecute(HttpResponse response) {
    super.onPostExecute(response);
    LeerAutentificacionXml l= new LeerAutentificacionXml(response);
    String s=l.Transformar();
    data.setData(Uri.parse(s.toString()));
}

もちろん、いくつかの参照 ( data?) をAsyncTask(コンストラクターなどを介して) に渡す必要があります。

于 2011-09-26T21:48:18.670 に答える
1

すべてのネットワーク コードを async タスクにカプセル化して、次のように呼び出すことができるようにしてください。

EditText txt_user = (EditText) findViewById(R.id.et_un);
EditText txt_pwd =  (EditText) findViewById(R.id.et_pw);
new LoginTask(txt_user.getText(), txt_pwd.getText(), this).execute();

LoginTask質問のコードに基づいて、次のようなものになります。

public class LoginTask extends AsyncTask<Void, Void, String> {

    // The URL probably won't change, so keep it in a static field
    private final static String URL = "http://....";

    private final String username;
    private final String password;
    private final Activity activity;

    /*
     * Pass all data required to log in and handle the result here.
     */
    public LoginTask(final String username, final String password, final Activity activity) {
        this.username = username;
        this.password = password;
        this.activity = activity;
    }

    /*
     * Do all the network IO and time-consuming parsing in here,
     * this method will be invoked in its own thread, not blocking the UI.
     */
    @Override
    protected String doInBackground(Void... params) {
        HttpClient client = getHttpClient();
        try {
            HttpEntity formEntity = new UrlEncodedFormEntity(Arrays.asList(
                    new BasicNameValuePair("usr", username),
                    new BasicNameValuePair("pass", password)));

            HttpPost request = new HttpPost(URL);
            request.setEntity(formEntity);

            HttpResponse response = client.execute(request);

            LeerAutentificacionXml l= new LeerAutentificacionXml(response);
            return l.Transformar();
        } catch (IOException ex) {
            // properly log your exception, don't just printStackTrace()
            // as you can't pinpoint the location and it might be cut off
            Log.e("LOGIN", "failed to log in", ex);
        } finally {
            client.getConnectionManager().shutdown();
        }
    }

    private HttpClient getHttpClient() {
        return null; // insert your previous code here
    }

    /*
     * This again runs on the UI thread, so only do here
     * what really needs to run on the UI thread.
     */
    @Override
    protected void onPostExecute(String response) {
        Intent data = new Intent();
        data.setData(Uri.parse(response));
        activity.setResult(Activity.RESULT_OK, data);
    }
}

ログインが「長時間実行されるタスク」であるとは考えていなくても、定義上、ネットワーク IO に関係するものはすべて、制御できない理由によりハングしたり、タイムアウトしたりする可能性があるタスクです。

LoginTask上記の例のように詳細を慎重にカプセル化する限り、ネットワーク IO を実行する複数のクラスを使用しても問題はありません。このタイプの2 つ、3 つのAsyncTaskクラスを作成してみて、共通部分を共通の「Web 要素」に抽出できるかどうかを確認してくださいAsyncTask。ただし、一度にすべてを実行しないでください。

于 2011-09-26T22:04:08.250 に答える