2014-09-28 72 views
0

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); 

      } 

     } 

} 

} 
+0

沒有看到所有的代碼和確切的錯誤,我假設問題是字符串成功只是在try塊內引用。如果是這種情況,請在try塊內聲明字符串。 Java特別針對未使用的變量,在您的情況下,它有可能從未涉及到。 – ffhaddad 2014-09-28 05:08:41

+0

你真的很確定String success = null會給你錯誤,而不是成功= json.getString(「success」);? – CChi 2014-09-28 05:09:03

+0

您在工作線程中訪問'getText'。如果只需要字符串,只需將它作爲參數傳遞 – Spurdow 2014-09-28 05:09:40

回答

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()); 
+0

已經嘗試。仍然有錯誤 – 2014-09-28 05:35:47

+0

,這就是即時通訊談論。:) – Spurdow 2014-09-28 05:37:10

+0

@AaronPaulMallavo所以發佈你的完整logcat – 2014-09-28 06:10:06

1

您需要返回從doInBackgroundonPostExecute String類型期待這一點。 doInBackground的返回結果還需要與AsyncTask聲明中的第三個參數相匹配,該聲明是onPostExecute的預期類型(AsyncTask上的每個參數都需要分別匹配onPreExecute,doInBackground和onPostExecute;請注意,前兩個參數是數組的數據類型)。

因此,您的return null將導致onPostExecute(String result)的問題。

有關AsyncTasks的更多信息,可以在開發人員指南中找到http://developer.android.com/reference/android/os/AsyncTask.html