0
我是新來的android和開發一個android應用程序,當點擊一個按鈕時,將該項目發佈到服務器,將搜索項目(由用戶提供到EditText中)發佈到服務器。正在使用的AsyncTask class.I有誤差與findViewById(它是未定義)。我的問題是在哪裏放置的onclick方法和views.Here的referncing是代碼Android Asynctask發佈到服務器錯誤
public class Server_Post extends AsyncTask<String, Void, String> {
private static final int REGISTRATION_TIMEOUT = 3 * 1000;
private static final int WAIT_TIMEOUT = 30 * 1000;
private final HttpClient httpclient = new DefaultHttpClient();
final HttpParams params = httpclient.getParams();
HttpResponse response;
Button Submit = (Button) findViewById(R.id.submitButton);
EditText textvalue = (EditText)findViewById(R.id.searcheditText);
//Onclick Listener
Submit.setOnClickListener(onClickListener)
private OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(final View v) {
switch(v.getId()){
case R.id.submitButton:
break;
}
};
protected void onPreExecute() {
//any code
}
@Override
protected String doInBackground(String... arg0) {
String URL = "url";
String username = "abc";
String password = "xyz";
try {
HttpConnectionParams.setConnectionTimeout(params, REGISTRATION_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, WAIT_TIMEOUT);
ConnManagerParams.setTimeout(params, WAIT_TIMEOUT);
HttpPost httpPost = new HttpPost(URL);
//Any other parameters you would like to set
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username",username));
nameValuePairs.add(new BasicNameValuePair("password",password));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Response from the Http Request
response = httpclient.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
//Check the Http Request for success
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
}
else{
//Closes the connection.
Log.w("HTTP1:",statusLine.getReasonPhrase());
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}catch (Exception e) {
}
return null;
}
protected void onCancelled() {
}
protected void onPostExecute(String content) {
}
private void connecttopostdata() {
Server_Post task = new Server_Post();
task.execute(textvalue.getText().toString());
}
}
響應是XML和我還希望在列表視圖上顯示響應。 在此先感謝。
這是一個解決方案,但IMO獲取數據的邏輯應該完全分開,並在不同的類中,並且使用'onPreExecute'方法中的參數傳遞的數據 – 2013-02-15 14:11:02