我是 Android 开发的初学者。我正在开发一个广泛中继 Web 服务调用的应用程序。第一个屏幕获取用户名和密码,并通过调用 Web 服务来验证用户。如果 U/P 有效,那么我需要启动第二个活动。在第二个活动中,我需要打 3 个电话。但是我还没有进入第二部分。事实上,我还没有完成完整的编码。但我想测试该应用程序是否可以正常工作。
调用 webservice 时,我正在显示警报对话框。但是该应用程序在某个地方崩溃了。LoginActivity 出现。当我输入 U/P 并按下登录按钮时,它崩溃了。
我的课程:
任务处理程序.java
public class TaskHandler {
private String URL;
private User userObj;
private String results;
private JSONDownloaderTask task; ;
public TaskHandler( String url, User user) {
this.URL = url;
this.userObj = user;
}
public String handleTask() {
Log.d("Two", "In the function");
task = new JSONDownloaderTask();
Log.d("Three", "In the function");
task.execute(URL);
return results;
}
private class JSONDownloaderTask extends AsyncTask<String, Void, String> {
private String username;// = userObj.getUsername();
private String password; //= userObj.getPassword();
public HttpStatus status_code;
public JSONDownloaderTask() {
Log.d("con", "Success");
this.username = userObj.getUsername();
this.password = userObj.getPassword();
Log.d("User" + this.username , " Pass" + this.password);
}
private AsyncProgressActivity progressbar = new AsyncProgressActivity();
@Override
protected void onPreExecute() {
progressbar.showLoadingProgressDialog();
}
@Override
protected String doInBackground(String... params) {
final String url = params[0]; //getString(R.string.api_staging_uri) + "Authenticate/";
// Populate the HTTP Basic Authentitcation header with the username and password
HttpAuthentication authHeader = new HttpBasicAuthentication(username, password);
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAuthorization(authHeader);
requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
try {
// Make the network request
Log.d(this.getClass().getName(), url);
ResponseEntity<Message> response = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<Object>(requestHeaders), Message.class);
status_code = response.getStatusCode();
return response.getBody().toString();
} catch (HttpClientErrorException e) {
status_code = e.getStatusCode();
return new Message(0, e.getStatusText(), e.getLocalizedMessage(), "error").toString();
} catch ( Exception e ) {
Log.d(this.getClass().getName() ,e.getLocalizedMessage());
return "Unknown Exception";
}
}
@Override
protected void onPostExecute(String result) {
progressbar.dismissProgressDialog();
switch ( status_code ) {
case UNAUTHORIZED:
result = "Invalid username or passowrd"; break;
case ACCEPTED:
result = "Invalid username or passowrd" + status_code; break;
case OK:
result = "Successful!"; break;
}
}
}
}
AsycProgressActivity.java
public class AsyncProgressActivity extends Activity {
protected static final String TAG = AsyncProgressActivity.class.getSimpleName();
private ProgressDialog progressDialog;
private boolean destroyed = false;
@Override
protected void onDestroy() {
super.onDestroy();
destroyed = true;
}
public void showLoadingProgressDialog() {
Log.d("Here", "Progress");
this.showProgressDialog("Authenticating...");
Log.d("Here", "afer p");
}
public void showProgressDialog(CharSequence message) {
Log.d("Here", "Message");
if (progressDialog == null) {
progressDialog = new ProgressDialog(this);
progressDialog.setIndeterminate(true);
}
Log.d("Here", "Message 2");
progressDialog.setMessage(message);
progressDialog.show();
}
public void dismissProgressDialog() {
if (progressDialog != null && !destroyed) {
progressDialog.dismiss();
}
}
}
登录活动.java
public class LoginActivity extends AsyncProgressActivity implements OnClickListener {
Button login_button;
HttpStatus status_code;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
login_button = (Button) findViewById(R.id.btnLogin);
login_button.setOnClickListener(this);
ViewServer.get(this).addWindow(this);
}
public void onDestroy() {
super.onDestroy();
ViewServer.get(this).removeWindow(this);
}
public void onResume() {
super.onResume();
ViewServer.get(this).setFocusedWindow(this);
}
public void onClick(View v) {
if ( v.getId() == R.id.btnLogin ) {
User userobj = new User();
String result;
userobj.setUsername( ((EditText) findViewById(R.id.username)).getText().toString());
userobj.setPassword(((EditText) findViewById(R.id.password)).getText().toString() );
TaskHandler handler = new TaskHandler(getString(R.string.api_staging_uri) + "Authenticate/", userobj);
Log.d(this.getClass().getName(), "One");
result = handler.handleTask();
Log.d(this.getClass().getName(), "After two");
Utilities.showAlert(result, LoginActivity.this);
}
}
实用程序.java
public class Utilities {
public static void showAlert(String message, Context context) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Login");
alertDialogBuilder.setMessage(message)
.setCancelable(false)
.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.dismiss();
//dialog.cancel();
}
});
alertDialogBuilder.setIcon(drawable.ic_dialog_alert);
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
}
编辑:更多信息
我尝试逐步添加 Logging 以查看其崩溃的位置。看起来它在这里崩溃了。因为在此之后它没有显示日志消息。
Log.d("Here", "Message");
if (progressDialog == null) {
progressDialog = new ProgressDialog(this);
progressDialog.setIndeterminate(true);
}
Log.d("Here", "Message 2");
Message 2
没有出现在日志中,但Message
出现了。