没有像我想的那样获得输出

时间:2014-06-14 22:54:53

标签: java php android html post

所以我从here获得了源代码来测试它,我没有得到任何来自服务器的输出或响应,就像我应该的那样。有谁知道为什么会这样?我有一种感觉,在第二次尝试"有些东西不起作用,它进入了exc的execption没有任何东西。它是否正确?无论如何,请帮助我得到这个给我一个回应/输出。

    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();

    }

1 个答案:

答案 0 :(得分:0)

您正在主(UI)线程上进行网络I / O.那是not allowed in 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();
}