为什么Toast类不能显示接收的数据

时间:2015-08-03 09:30:03

标签: android

我想按下按钮,结果我看到使用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);

        } 

1 个答案:

答案 0 :(得分:0)

你需要使用show()

while(kbhit()) getch();