我从 android 开始(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();
} }
这是我连接到网络服务的课程。
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 失败。
提前谢谢。