我正在研究一个项目,我无法摆脱这个令人讨厌的问题:我在用户输入他的名字和密码后显示了一个Toast消息(如果是注册过程则发电子邮件)以及之后应用程序检查数据(如果是注册过程,则将其存储在数据库中)。或者至少我想这样。但实际上,一旦打开应用程序就会显示消息,导致出现“Benvenuto NULL”消息,显然它应该说“Benvenuto [USERNAME]”...我尝试使用OnComplete,但Eclipse说我:“方法onComplete( BundA)类型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);
}
}
P.S。:我是意大利人,所以如果你读到一些你没有得到的东西,请不要惊慌xD
答案 0 :(得分:0)
您需要将Toast.make(...).show()
移动到onClick
处理程序中。
作为旁注,请避免在应用中使用硬编码字符串。使用android的资源机制(res / values / strings.xml) - 它将改变您的应用程序的输出或将其本地化为更容易的其他语言。