Im在asynctask中遇到問題。有致命的錯誤。這是發生錯誤的部分。它告訴我字符串的成功未被使用。整個代碼現在已發佈。幫助pleaseIm在asynctask中遇到麻煩。有致命的錯誤。這是發生錯誤的部分。它告訴我字符串的成功未被使用。整個代碼現在已發佈。幫助pleaseIm在asynctask中遇到麻煩。有致命的錯誤。這是發生錯誤的部分。它告訴我字符串的成功未被使用。整個代碼現在已發佈。幫助pleaseIm在asynctask中遇到麻煩。有致命的錯誤。這是發生錯誤的部分。它告訴我字符串的成功未被使用。整個代碼現在已發佈。請幫助asyncTask#1中的錯誤
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);
}
}
}
}
沒有看到所有的代碼和確切的錯誤,我假設問題是字符串成功只是在try塊內引用。如果是這種情況,請在try塊內聲明字符串。 Java特別針對未使用的變量,在您的情況下,它有可能從未涉及到。 – ffhaddad 2014-09-28 05:08:41
你真的很確定String success = null會給你錯誤,而不是成功= json.getString(「success」);? – CChi 2014-09-28 05:09:03
您在工作線程中訪問'getText'。如果只需要字符串,只需將它作爲參數傳遞 – Spurdow 2014-09-28 05:09:40