我在asynctask中遇到麻烦。有致命的错误。这是发生错误的部分。它告诉我String成功未使用。整个代码现已发布。请帮助我在asynctask中遇到麻烦。有致命的错误。这是发生错误的部分。它告诉我String成功未使用。整个代码现已发布。请帮助我在asynctask中遇到麻烦。有致命的错误。这是发生错误的部分。它告诉我String成功未使用。整个代码现已发布。请帮助我在asynctask中遇到麻烦。有致命的错误。这是发生错误的部分。它告诉我String成功未使用。整个代码现已发布。请帮忙
package com.example.ram2;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends Activity implements OnClickListener {
EditText user, pass;
Button mSubmit, mRegister;
ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://10.0.2.2/FuckAnd/login.php";
static final String TAG_SUCCESS = "success";
static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
user = (EditText) findViewById(R.id.username);
pass = (EditText) findViewById(R.id.password);
mSubmit = (Button) findViewById(R.id.login);
mSubmit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new AttemptLogin().execute();
}
public class AttemptLogin extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Logging in...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
pDialog.dismiss();
finish() ;
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
String username = user.getText().toString();
String password = pass.getText().toString();
String success = null;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
params);
Log.d("Login attempt", json.toString());
success= json.getString("success");
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
//JSONObject json;
// dismiss the dialog once product deleted
pDialog.dismiss();
//if (TAG_SUCCESS == null) {
if (result.equals("success")) {
//if (success.equals(success)) {
//Log.d("Login Successful!", json.toString());
Toast.makeText(Login.this, "Login Successful!", Toast.LENGTH_SHORT).show();
//Toast.makeText(LoginActivity.this, "Login Successful!", Toast.LENGTH_LONG).show();
//Intent i = new Intent(LoginActivity.this, PortalContents.class);
finish();
//startActivity(i);
//return null;
}
else {
//Log.d("Login Failure!", json.getString(TAG_MESSAGE));
Toast.makeText(Login.this, "Login Fail!", Toast.LENGTH_LONG).show();
//return json.getString(TAG_MESSAGE);
//return json.getString(TAG_MESSAGE);
}
}
}
}
答案 0 :(得分:1)
您需要从doInBackground
返回onPostExecute
所期望的类型字符串。从doInBackground
返回也需要匹配AsyncTask声明中的第三个参数,它是onPostExecute
的预期类型(AsyncTask上的每个参数需要分别匹配onPreExecute,doInBackground和onPostExecute;请注意前两个是数据类型的数组。)
因此return null
会导致onPostExecute(String result)
的问题。
有关AsyncTasks的更多信息,请参阅http://developer.android.com/reference/android/os/AsyncTask.html的开发者指南。
答案 1 :(得分:0)
试试这个:
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
String username = arg[0];
String password = arg[1];
String success = null; //this keeps getting error
//...
}
和
new Task().execute(user.getText().toString(),pass.getText().toString());