私はアンドロイド(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 の失敗が起こっているかどうかを知りたいです。
事前にthx。