我正在創建一個運行Windows服務的簡單系統,其中包含一個用戶表,並且我想通過向服務發送一些內容來驗證某人的登錄憑據。我正在嘗試發送他們的用戶名和密碼,但我一直收到與我的異步任務有關的錯誤。我知道你不應該在那裏混淆UI的東西,我不是。原本我打電話給那裏的另一項活動,但我評論說。現在doInBackground中唯一的事情是如果驗證是好的,則將布爾值設置爲true。在那裏,我讀取了異步執行後的值,然後將一個捆綁到一起移動到下一個地方。我只是不知道這裏有什麼問題。自從我在Android中進行編程已經有一段時間了,所以也許我錯過了一些愚蠢的東西。如果有人能幫助我,我將不勝感激!謝謝。將這些信息發送給服務可能也是一個問題?客戶端服務的Android異步任務錯誤
更新:在清單中添加互聯網使用情況後,如果我在doInBackground中的程序中有log.d,它會打印出來。如果我沒有它,結果值保持錯誤。它看起來像有一些問題,我的服務和Android應用之間的連接...
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends Activity {
private static final int DATA_FROM_MAIN_ACTIVITY = 1;
private static final String SERVICEURL = "http://localhost:56638/ClientService.svc";
private EditText userTextBox, passTextBox;
private Button loginButton;
private String uName, pass;
private Boolean result = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
userTextBox = (EditText) findViewById(R.id.userTextBox);
passTextBox = (EditText) findViewById(R.id.passTextBox);
loginButton = (Button) findViewById(R.id.loginButton);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uName = userTextBox.getText().toString();
pass = passTextBox.getText().toString();
SendLoginMessage task = new SendLoginMessage();
task.execute(uName, pass);
if (result.equals(true)) {
Intent goToMainScreen = new Intent(getApplicationContext(),
MainActivity.class);
goToMainScreen.putExtra("username", uName);
goToMainScreen.putExtra("password", pass);
startActivityForResult(goToMainScreen,
DATA_FROM_MAIN_ACTIVITY);
} else {
Toast.makeText(
getApplicationContext(),
"There was an issue with your username or password.",
Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class SendLoginMessage extends AsyncTask<String, String, Void> {
@Override
protected void onPreExecute() {
// Log.d("message almost at server, " + textFromArea, selected,
// null);
}
@Override
protected Void doInBackground(String... names) {
ArrayList<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("username", names[0]));
postParams.add(new BasicNameValuePair("password", names[1]));
String response = null;
try {
response = HttpClient.executeHttpPost(SERVICEURL, postParams);
String newResponse = response.toString();
newResponse = newResponse.replaceAll("\\s+", "");
// if user was authenticated...
if (newResponse.equals(true)) {
result = true;
// creating an intent to take user to next page.
// load their DB objects on the
// on create in other activity
// pass the username/password to next activity
// then make a request to the server for their database
// objects.
// Intent goToMainScreen = new
// Intent(getApplicationContext(), MainActivity.class);
// goToMainScreen.putExtra("username", names[0]);
// goToMainScreen.putExtra("password", names[1]);
// startActivityForResult(goToMainScreen,
// DATA_FROM_MAIN_ACTIVITY);
}
} catch (Exception e) {
Log.d("ERROR", "exception in background");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Toast.makeText(getApplicationContext(),
// .show();
}
}
}
你一直在收到什麼錯誤? – codeMagic 2013-03-13 19:30:46
這可能是一個問題'if(newResponse.equals(true))'它應該是if(newResponse.equals(「true」)),因爲您現在正在比較'Strings' – codeMagic 2013-03-13 19:32:16
我還在我的清單中添加了互聯網使用,但這並沒有解決問題。我也將.equals更改爲字符串檢查,但這也沒有任何區別。 – Tastybrownies 2013-03-13 19:36:20