没有得到我想要的输出


Not getting an output like im supposed to

所以我得到了源代码从这里测试出来,我没有得到任何输出或响应从服务器像我应该。有人知道为什么会这样吗?我有一种感觉,在第二次"尝试"时,有些东西不起作用,它会进入异常ex,里面什么都没有。这是正确的吗?无论如何,请帮助我得到这个给我一个响应/输出。

    public class HttpPostExample extends Activity {
  TextView content;
  EditText fname, email, login, pass;
  String Name, Email, Login, Pass; 
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_http_post_example);
      content    =   (TextView)findViewById( R.id.content );
      fname      =   (EditText)findViewById(R.id.name);
      email      =   (EditText)findViewById(R.id.email);
      login      =    (EditText)findViewById(R.id.loginname);
      pass       =   (EditText)findViewById(R.id.password);

      Button saveme=(Button)findViewById(R.id.save);
      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();
      Email   = email.getText().toString();
      Login   = login.getText().toString();
      Pass   = pass.getText().toString();
       // Create data variable for sent values to server  
        String data = URLEncoder.encode("name", "UTF-8") 
                     + "=" + URLEncoder.encode(Name, "UTF-8"); 
        data += "&" + URLEncoder.encode("email", "UTF-8") + "="
                    + URLEncoder.encode(Email, "UTF-8"); 
        data += "&" + URLEncoder.encode("user", "UTF-8") 
                    + "=" + URLEncoder.encode(Login, "UTF-8");
        data += "&" + URLEncoder.encode("pass", "UTF-8") 
                    + "=" + URLEncoder.encode(Pass, "UTF-8");
        String text = "";
        BufferedReader reader=null;
        // Send data 
      try
      { 
          // Defined URL  where to send data
          URL url = new URL("http://androidexample.com/media/webservice/httppost.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
      content.setText( text  );
  }

}

编辑:我只是做了一些调试,发现我是对的,问题是,它在这里的某个地方,有人知道确切的地方吗?

     URL url = new URL("http://androidexample.com/media/webservice/httppost.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();
    }

您正在主(UI)线程上执行网络I/O。这在Android是不允许的。

您应该将所有代码移动到AsyncTask。但是,请记住,您不能从后台线程更改UI,因此显示结果的部分必须在onPostExecute()中执行。

例如,将GetText()方法更改为返回带有文本的字符串而不是调用content.setText(),并将单击侦听器更改为如下内容:

public void onClick(View v)
{
    // Get user defined values
    Name = fname.getText().toString();
    Email = email.getText().toString();
    Login = login.getText().toString();
    Pass = pass.getText().toString();
    new AsyncTask<Void, Void, String>()
    {
         @Override
         public String doInBackground (Void... params)
         {
             return GetText();
         }
         @Override
         protected void onPostExecute(String result)
         {
             content.setText(result);
         }
    }.execute();
}