android - EditText 给出奇怪的字符串输出

标签 android string android-asynctask

我不明白为什么 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)
{

}
}

最佳答案

以下错误

etHost.toString().trim();

喜欢

etHost.getText().toString().trim();

关于android - EditText 给出奇怪的字符串输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19386889/

相关文章:

android - 带有航路点的 Directions API 总距离和持续时间

c - 当我尝试增加 char 的 ascii 值时,为什么会得到随机数?

java - 在AsyncTask中添加Marker到map api v2

Android Studio Stuck at Gradle 下载创建新项目

java - 从 C 尽快填充 Android 位图中的数据

java - 如何从Java中的字符串中提取部分?

javascript - 如何使用 jQuery 删除跨度数值的 25%?

android-activity - 当应用程序处于后台时,Android 异步任务不会更新 UI

android - 如果 AsyncTask 显然没有完成执行,中断 AsyncTask 的好方法是什么?

java - 为什么 Facebook SDK 无法导入我的 Android 项目?