问题描述
我登录系统的这方面工作得很好,如果我有return语句设置为0或1,但如果我使用空失败。这都是改编自其中,这个特殊的回报看起来下面列出我自己的code。
公共LoginTask(投票活动,ProgressDialog progressDialog)
{
this.activity =活动;
this.progressDialog = progressDialog;
}保护整数doInBackground(字符串...为arg0){
userName的的EditText =(EditText上)activity.findViewById(R.id.emailEditText);
的EditText passwordEdit =(EditText上)activity.findViewById(R.id.passEditText);
。字符串email = userName.getText()的toString();
字符串密码= passwordEdit.getText()的toString()。
UserFunctions userFunction =新UserFunctions();
JSONObject的JSON = userFunction.loginUser(电子邮件,密码);
progressDialog.dismiss();
//检查登录响应
//Log.v(\"test,Integer.toString(jsonParser.getResponse code()));
尝试{
如果(json.getString(KEY_SUCCESS)!= NULL){
//loginErrorMsg.setText();
//loginFragment.loginErrorMsg.setText(\"Success);
字符串资源= json.getString(KEY_SUCCESS); 如果(的Integer.parseInt(RES)== 1){
//用户成功登录
在SQLite数据库//存储用户详细信息
数据库处理器分贝=新数据库处理器(activity.getApplicationContext());
JSONObject的json_user = json.getJSONObject(用户);
//Log.v(\"name,json_user.getString(KEY_NAME));
//清除数据库中的所有previous数据
userFunction.logoutUser(activity.getApplicationContext());
db.addUser(json_user.getString(KEY_NAME),json_user.getString(KEY_EMAIL)
json.getString(KEY_UID),json_user.getString(KEY_CREATED_AT));
//关闭登录屏幕
//完();
// loginErrorMsg =(TextView中)activity.findViewById(R.id.loginErrorMsg);
//loginErrorMsg.setText(\"logged在);
//passwordEdit.setText();
}其他{
//错误的登录
//progressDialog.setMessage(\"Incorrect用户名或密码);
//loginErrorMsg.setText(\"Incorrect用户名/密码);
} } }赶上(NullPointerException异常五){
e.printStackTrace(); }
赶上(JSONException E){
e.printStackTrace();
} 返回1;
}
这是我用过,看看反应code教程:
保护整数doInBackground(字符串...为arg0)
{
字符串结果=;
INT响应code = 0;
尝试
{
HttpClient的客户端=新DefaultHttpClient();
HttpPost httppost =新HttpPost(http://www.256design.com/projectTransparency/project/headerLogin.php); 清单<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;();
nameValuePairs.add(新BasicNameValuePair(EMAILADDRESS,为arg0 [0]));
nameValuePairs.add(新BasicNameValuePair(密码,将arg0 [1]));
httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中)); INT executeCount = 0;
HTT presponse响应;
做
{
progressDialog.setMessage(登录。(+(executeCount + 1)+/ 5));
//执行HTTP POST请求
executeCount ++;
响应= client.execute(httppost);
。响应code = response.getStatusLine()的getStatus code();
//如果你想看到的响应code,就可以登录其
//在这里致电:
// Log.d(256设计,状态code:+响应code)
}而(executeCount小于5&安培;&安培;响应code == 408); RD的BufferedReader =新的BufferedReader(新的InputStreamReader(
。response.getEntity()的getContent())); 串线;
而((行= rd.readLine())!= NULL)
{
结果= line.trim();
}
ID =的Integer.parseInt(结果);
}
赶上(例外五){
响应code = 408;
e.printStackTrace();
}
返回响应code;
}
其目的是通过你的工作结果(这是一个工作线程执行)为 onPostExecute
中,为了处理所述UI线程上的结果。如果你想在响应成功作业运行更新用户界面这是必需的。
This aspect of my login system works just fine if I have the return statement set to 0 or 1, but fails if I use null. This is all adapted from http://256design.com/blog/android-login-asynctask/ where this particular return looks as listed below my own code.
public LoginTask(Polling activity, ProgressDialog progressDialog)
{
this.activity = activity;
this.progressDialog = progressDialog;
}
protected Integer doInBackground(String... arg0) {
EditText userName = (EditText)activity.findViewById(R.id.emailEditText);
EditText passwordEdit = (EditText)activity.findViewById(R.id.passEditText);
String email = userName.getText().toString();
String password = passwordEdit.getText().toString();
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.loginUser(email, password);
progressDialog.dismiss();
// check for login response
//Log.v("test", Integer.toString(jsonParser.getResponseCode()));
try {
if (json.getString(KEY_SUCCESS) != null) {
//loginErrorMsg.setText("");
//loginFragment.loginErrorMsg.setText("Success");
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
//user successfully logged in
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(activity.getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
//Log.v("name", json_user.getString(KEY_NAME));
// Clear all previous data in database
userFunction.logoutUser(activity.getApplicationContext());
db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL),
json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Close Login Screen
//finish();
//loginErrorMsg = (TextView)activity.findViewById(R.id.loginErrorMsg);
//loginErrorMsg.setText("logged in");
//passwordEdit.setText("");
}else{
// Error in login
//progressDialog.setMessage("Incorrect username or password");
//loginErrorMsg.setText("Incorrect username/password");
}
}
} catch (NullPointerException e) {
e.printStackTrace();
}
catch (JSONException e) {
e.printStackTrace();
}
return 1;
}
The tutorial that I used, take a look at responseCode:
protected Integer doInBackground(String... arg0)
{
String result = "";
int responseCode = 0;
try
{
HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.256design.com/projectTransparency/project/headerLogin.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("emailAddress", arg0[0]));
nameValuePairs.add(new BasicNameValuePair("password", arg0[1]));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
int executeCount = 0;
HttpResponse response;
do
{
progressDialog.setMessage("Logging in.. ("+(executeCount+1)+"/5)");
// Execute HTTP Post Request
executeCount++;
response = client.execute(httppost);
responseCode = response.getStatusLine().getStatusCode();
// If you want to see the response code, you can Log it
// out here by calling:
// Log.d("256 Design", "statusCode: " + responseCode)
} while (executeCount < 5 && responseCode == 408);
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String line;
while ((line = rd.readLine()) != null)
{
result = line.trim();
}
id = Integer.parseInt(result);
}
catch (Exception e) {
responseCode = 408;
e.printStackTrace();
}
return responseCode;
}
The purpose is to pass the result of your job (which is executed on a worker thread) to onPostExecute
, in order to process the result on the UI thread. This is required if you want to update the user interface in response to a successful job run.
这篇关于什么是return语句中的AsyncTask的doInBackground()的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!