【问题标题】:Why Toast class does not work to display the received data [duplicate]为什么 Toast 类不能显示接收到的数据 [重复]
【发布时间】:2015-08-03 09:30:03
【问题描述】:

我希望当我按下按钮时,我会看到使用 Toast 类从服务器接收到的文本。我的php 代码是正确的,因为当我使用content.setText(text); 而不是Toast.makeText(getApplication(),text,Toast.LENGTH_LONG); 时,当我按下按钮时,我可以看到来自php 代码的接收文本。但我不想使用TextView 类来显示收到的文本。我想使用Toast 类显示收到的文本。

public class MainActivity extends Activity {
    TextView content;
    EditText fname;
    String Name;
    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            content    =   (TextView)findViewById( R.id.textView );
            fname      =   (EditText)findViewById(R.id.editText);



            Button saveme=(Button)findViewById(R.id.button);

            saveme.setOnClickListener(new Button.OnClickListener(){

                public void onClick(View v)
                {
                    try{

                        // CALL GetText method to make post method call
                        GetText();
                    }
                    catch(Exception ex)
                    {
                        content.setText(" url exeption! " );
                    }
                }
            });}


        // Create GetText Metod
        public  void  GetText()  throws UnsupportedEncodingException
        {
            // Get user defined values
            Name = fname.getText().toString();


            // Create data variable for sent values to server

            String data = URLEncoder.encode("name", "UTF-8")
                    + "=" + URLEncoder.encode(Name, "UTF-8");


            String text = "";
            BufferedReader reader=null;

            // Send data
            try
            {

                // Defined URL  where to send data
                URL url = new URL("http://127.0.0.1:8080/apps/register.php");

                // Send POST data request

                URLConnection conn = url.openConnection();
                conn.setDoOutput(true);
                OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
                wr.write( data );
                wr.flush();

                // Get the server response

                reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                StringBuilder sb = new StringBuilder();
                String line = null;

                // Read Server Response
                while((line = reader.readLine()) != null)
                {
                    // Append server response in string
                    sb.append(line + "\n");
                }


                text = sb.toString();
            }
            catch(Exception ex)
            {

            }
            finally
            {
                try
                {

                    reader.close();
                }

                catch(Exception ex) {}
            }

            // Show response on activity

            Toast.makeText(getApplication(),text,Toast.LENGTH_LONG);

        } 

【问题讨论】:

  • 你谷歌了吗?
  • 这个网站上有很多关于这个的答案。以及无数的网络解释它。

标签: android


【解决方案1】:

你需要使用 show()

Toast.makeText(getApplication(),text,Toast.LENGTH_LONG).show();

【讨论】:

    猜你喜欢
    • 2020-02-29
    • 1970-01-01
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多