2014-02-28 19 views
0

我正在一個項目上工作,我無法擺脫這個惱人的問題:我做了一個敬酒信息顯示後,用戶輸入他的名字和密碼(和電子郵件,如果它是一個註冊過程)以及在應用程序檢查數據之後(或者如果它是註冊過程,則將其存儲在數據庫中)。或者至少我是這樣想的。但實際上,只要我打開應用程序導致出現「Benvenuto NULL」消息時,顯然應該說「Benvenuto [USERNAME]」...我嘗試使用OnComplete,但Eclipse說:「方法onComplete(束)型MainActivity必須覆蓋或實現超法」 .......在一個類中的時間順序

public class MainActivity extends Activity { 
Button reg, log, fb; 
EditText username, password, email; 
String stringa; 
String nome; 
private final int SPLASH_DISPLAY_LENGHT = 1000; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.activity_main); 
    reg = (Button) findViewById(R.id.button1); 
    log = (Button) findViewById(R.id.button2); 
    username = (EditText) findViewById(R.id.username); 
    password = (EditText) findViewById(R.id.password); 
    email = (EditText) findViewById(R.id.email); 
    log.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View view) { 
      Boolean f = false; 
      EditText txtmail = (EditText) findViewById(R.id.username); 
      String mail = txtmail.getText().toString(); 
      EditText txtpass = (EditText) findViewById(R.id.password); 
      String pas = txtpass.getText().toString(); 

      String result = ""; 
      ArrayList<NameValuePair> dati = new ArrayList<NameValuePair>(); 
      dati.add(new BasicNameValuePair("email", mail)); 
      dati.add(new BasicNameValuePair("password", pas)); 
      InputStream is = null; 

      try { 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost(
         "http://tripleleon.altervista.org/login.php"); 
       httppost.setEntity(new UrlEncodedFormEntity(dati)); 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
       is = entity.getContent(); 

      } catch (Exception e) { 
       Log.e("log_REMOTO", 
         "Errore nella connessione HTTP: " + e.toString()); 
       // Toast.makeText(this, "Connessione non riuscita", 
       // Toast.LENGTH_SHORT).show(); 
      } 

      // convert response to string 

      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(); 
       result = sb.toString(); 

      } catch (Exception e) { 
       Log.e("log_REMOTO", 
         "Errore nella conversione del risultato: " 
           + e.toString()); 
      } 

      // paring data 

      try { 
       JSONArray jArray = new JSONArray(result); 
       for (int i = 0; i < jArray.length(); i++) { 
        JSONObject json_data = jArray.getJSONObject(i); 
        nome = json_data.getString("idutente"); 
        Log.i("log_tag", 
          ", nome: " + json_data.getString("idutente") 
            + ", email: " 
            + json_data.getString("email") 
            + ", password: " 
            + json_data.getString("password") 
            + ", eta: " 
            + json_data.getString("age")); 

       } 

      } catch (JSONException e1) { 
       Log.e("log_REMOTO", "Errore json: " + e1.toString()); 
       f = true; 
      } catch (Exception e1) { 
       Log.e("log_REMOTO", "Errore JSON: " + e1.toString()); 
       e1.printStackTrace(); 
      } 
     } 
    }); 

    Toast.makeText(this, "Benvenuto " + nome, Toast.LENGTH_SHORT).show(); 
    reg.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      InputStream is; 
      if (username.getText().length() == 0) { 
       username.setText("Enter Username"); 
      } else if (password.getText().length() == 0) { 
       password.setText("Enter Password"); 
      } else if (email.getText().length() == 0) { 
       email.setText("Enter email"); 
      } else { 
       try { 
        String a = username.getText().toString().trim(); 
        String b = password.getText().toString().trim(); 
        String c = email.getText().toString().trim(); 
        HttpClient httpclient = new DefaultHttpClient(); 
        HttpPost httppost = new HttpPost(
          "http://tripleleon.altervista.org/register.php"); 
        ArrayList<NameValuePair> dati = new ArrayList<NameValuePair>(); 
        dati.add(new BasicNameValuePair("idutente", a)); 
        dati.add(new BasicNameValuePair("password", b)); 
        dati.add(new BasicNameValuePair("email", c)); 
        httppost.setEntity(new UrlEncodedFormEntity(dati, 
          HTTP.UTF_8)); 

        HttpResponse response = httpclient.execute(httppost); 
        HttpEntity entity = response.getEntity(); 
        is = entity.getContent(); 
        username.setText(""); 
        password.setText(""); 
        email.setText(""); 
       } catch (Exception e) { 
        Log.e("log_tag", 
          "Error in http connection " + e.toString()); 
        Toast.makeText(getApplicationContext(), 
          "Connection error", Toast.LENGTH_SHORT).show(); 
       } 
      } 
     } 
    }); 
} 

    @Override 
     public void onComplete(Bundle b){ 
     startActivity(main); 
    } 
} 

PS:我是意大利人,所以如果你讀的東西不要驚慌,你沒有得到的xD

+1

你可以發佈你的代碼嗎? – Giacomoni

+0

如果名稱屬性== null,則不要調用toast.show()。但我想你有一個結構性問題。沒有代碼細節,不能提供更詳細的幫助。 –

+0

@StanSmith這是我在其他一些活動中設置的屬性,其中時間順序並不那麼重要。但在這種情況下,它將無法正常工作,導致應用程序啓動後的敬酒顯示;所以如果它是空的,它不會出現在所有的情況下,無論是在開始時還是在登錄後... –

回答

0

您需要將Toast.make(...).show()移至您的onClick處理程序。

請注意,請避免在您的應用中使用硬編碼的字符串。使用android的資源機制(res/values/strings.xml) - 它可以改變你的應用程序的輸出,或者將它本地化爲不同的語言。

相關問題