我不明白为什么 EditText 给了我一个奇怪的值,它保存在我使用的变量中。
例如,当我将这些 EditText 的内容保存到某些变量时,会保存此值:
variable: android.widget.EditText{41940958 VFED..CL .F...... 24,158-456,225 #7f080002 app:id/etHost}
这是我的代码:
public class MainActivity extends Activity {
Button btnConnetti;
EditText etUsername;
EditText etPassword;
EditText etHost;
String host=null;
String username=null;
String password=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnConnetti = (Button) findViewById(R.id.btnConnetti);
etUsername = (EditText) findViewById(R.id.etUsername);
etPassword = (EditText) findViewById(R.id.etPassword);
etHost = (EditText) findViewById(R.id.etHost);
btnConnetti.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
host = etHost.toString().trim();
username = etUsername.toString().trim();
password = etPassword.toString().trim();
Log.d("deb","host: "+etHost.toString());
if(host.isEmpty() || username.isEmpty() || password.isEmpty())
{
new AlertDialog.Builder(getApplicationContext())
.setTitle("Login fallito!")
.setMessage("Compilare tutti i campi richiesti.")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
return;
}
}).create().show();
}
else
{
myClickHandler(getCurrentFocus());
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void myClickHandler(View view) {
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
new InviaDati(host,username,password).execute();
} else {
// display error
}
}
}
class InviaDati extends AsyncTask<String, Integer, Void>
{
InputStream is = null;
String host,username,password;
public InviaDati(String host,String username,String password)
{
this.host=host;
this.username=username;
this.password=password;
}
@Override
protected void onPreExecute()
{
}
@Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
Log.d("deb",host+"/callback.php?username="+username+"&password="+password);
try {
URL url = new URL(host+"/callback.php?username="+username+"&password="+password);
Log.d("deb",host+"/callback.php?username="+username+"&password="+password);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
int response = conn.getResponseCode();
Log.d("DEBUG_TAG", "The response is: " + response);
is = conn.getInputStream();
// Makes sure that the InputStream is closed after the app is
// finished using it.
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
}
}