我已经在这个问题上坚持了几天,我希望progresdialog能够展示,但它并没有显示出来。当我登录时,我希望asyncHttpTask显示并关闭processdialog,但这并不会发生。
我的活动登录中的clicklogin方法
@OnClick(R.id.login)
public void clickLogin() {
if ((!etUsername.getText().toString().equals("")) && (!etPassword.getText().toString().equals(""))) {
boolean succesfullLogin=new UserFunctions().
loginUser(etUsername.getText().toString(),
etPassword.getText().toString(),Login.this);
if(succesfullLogin){
Intent intent = new Intent(getApplicationContext(), Home.class);
startActivity(intent);
finish();
}else{
}
} else if ((!etPassword.getText().toString().equals(""))) {
Toast.makeText(getApplicationContext(),
"Password field empty", Toast.LENGTH_SHORT).show();
} else if ((!etUsername.getText().toString().equals(""))) {
Toast.makeText(getApplicationContext(),
"Username field empty", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),
"Username and Password field are empty", Toast.LENGTH_SHORT).show();
}
}
类userFunctions中的loginuser,它调用异步任务
public Boolean loginUser(String username, String password, Login context) {
ProgressDialog pDialog = new ProgressDialog(context);
pDialog.setTitle("Contacting Servers");
pDialog.setMessage("Logging in ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
Map<String, String> map = new HashMap<>();
map.put("url", loginURL);
map.put("username", username);
map.put("password", password);
AsyncHttpTask loginTask = new AsyncHttpTask(pDialog);
try {
HttpObject response = loginTask.execute(map).get();
if (response.getResponseCode() == 200) {
JSONObject json = response.getJsonObject();
DatabaseHandler db = new DatabaseHandler(context);
Log.i("json user:", json.getString("username"));
/**
* Clear all previous data in SQlite database.
**/
UserFunctions logout = new UserFunctions();
logout.logoutUser(context);
//db.addUser(json_user.getString(KEY_FIRSTNAME), json_user.getString(KEY_LASTNAME), json_user.getString(KEY_EMAIL),
// json_user.getString(KEY_USERNAME), json_user.getInt(KEY_AGE), json_user.getInt(KEY_NRCHILDREN),
// json_user.getString(KEY_CIVILSTATUS), json_user.getString(KEY_SEX), json_user.getInt(KEY_VEGETARIAN) == 0 ? false : true);
Log.i("succes", "succesfull log in");
return true;
} else {
Toast.makeText(context,
"Wrong username or password", Toast.LENGTH_SHORT).show();
Log.i("errormessage", "Incorrect username/password");
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
最后是AsyncHttpTask,它扩展了Asynctask
public class AsyncHttpTask extends AsyncTask<Map<String, String>, Integer, HttpObject> {
private ProgressDialog pDialog;
private HttpObject httpObject;
private HttpURLConnection urlConnection = null;
public AsyncHttpTask(ProgressDialog pDialog) {
this.pDialog = pDialog;
httpObject = new HttpObject();
}
@Override
protected void onPreExecute() {
//super.onPreExecute();
pDialog.setMessage("Doing something, please wait.");
pDialog.show();
}
@Override
protected HttpObject doInBackground(Map<String, String>... params) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
buildConnection(params[0]);
httpObject.setResponseCode(urlConnection.getResponseCode());
if (httpObject.getResponseCode() == 200) {
buildJson(new BufferedInputStream(urlConnection.getInputStream()));
}
} catch (IOException e) {
Log.i("HttpTask:",e.getMessage());
e.printStackTrace();
}
return httpObject;
}
private void buildConnection(Map<String, String> urlMap) throws IOException {
URL url = new URL(urlMap.get("url"));
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
for (String key : urlMap.keySet()) {
if (key != "url") {
urlConnection.setRequestProperty(key, urlMap.get(key));
}
}
Log.i("HttpTask", urlConnection.toString());
Log.i("HttpTask", url.toString());
}
private void buildJson(BufferedInputStream is) {
String json = "";
JSONObject jObj = null;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
Log.i("json in string: ", json);
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
Log.i("json in string: ", json);
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
httpObject.setJsonObject(jObj);
}
@Override
protected void onPostExecute(HttpObject httpObject) {
super.onPostExecute(httpObject);
pDialog.dismiss();
}
}
感谢您帮助我:)
答案 0 :(得分:0)
问题是我不理解asynctask
这就是我做错了:
HttpObject response = loginTask.execute(map).get()