post数组为空,但file_get_content返回值

时间:2015-01-26 14:08:53

标签: php android apache post

我写了一个简单的应用程序,从我的Android应用程序获取信息,并将其发布在我简单的PHP服务器上。但奇怪的是它将结果保存在一个html文件中并显示出来。但是当我尝试回显或打印它时,post值为空。真的很令人沮丧。这是PHP代码,如果你可以帮助我,我很感激:

    // get the "message" variable from the post request
   // this is the data coming from the Android app
      <?php
  $message=$_POST["message"];
   switch ($message) {
  case "Peter":
    echo "Your favorite friend is Peter!";
    break;
case "Joe":
    echo "Your favorite friend is Joe!";
    break;
case "Ben":
    echo "Your favorite friend is Ben!";
    break;
default:
    echo "Your favorite friend is neither !"."<br/>";
 }
  $filename="androidloops.html";
// write (append) the data to the file
 file_put_contents($filename,$message."<br />",FILE_APPEND);
   // load the contents of the file to a variable
 $androidloops=file_get_contents($filename);
 // display the contents of the variable (which has the contents of the file)
  echo $androidloops;
  var_dump($message); 
 ?>   

这是我的应用程序的java代码:

 package com.example.sendingapp;
 import java.io.IOException;
 import org.apache.http.client.ClientProtocolException;
 import java.util.ArrayList;
 import java.util.List;
 import org.apache.http.client.HttpClient;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.impl.client.DefaultHttpClient;


 import org.apache.http.NameValuePair;
 import org.apache.http.client.entity.UrlEncodedFormEntity;
   import org.apache.http.message.BasicNameValuePair;
   // import everything you need
  import android.app.Activity;
    import android.content.Intent;
   import android.os.Bundle;
  import android.os.StrictMode;
  import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.Toast;
  public class MainActivity extends Activity {
   Button sendButton,create;
   EditText msgTextField,num;
 /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    // load the layout
    setContentView(R.layout.activity_main);        
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  StrictMode.setThreadPolicy(policy); 
    // make message text field object
    msgTextField = (EditText) findViewById(R.id.msgTextField);
    num = (EditText) findViewById(R.id.editText1);
    // make send button object
    sendButton = (Button) findViewById(R.id.sendButton);
    create= (Button) findViewById(R.id.button1);
        create.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        Intent openstartingpoint=new Intent("com.example.sendingapp.create");
            startActivity(openstartingpoint);
        }
    });
   }
    // this is the function that gets called when you click the button
     public void send(View v)
    {
    // get the message from the message text box
     String msg = msgTextField.getText().toString(); 
    String msg2 = num.getText().toString(); 
    // make sure the fields are not empty
    if (msg.length()>0)
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://192.168.1.105/looper.php");
     try {
            List<NameValuePair> nameValuePairs = new      ArrayList<NameValuePair>(2);
         nameValuePairs.add(new BasicNameValuePair("message", msg));
          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
       httpclient.execute(httppost);
       msgTextField.setText(""); // clear text box
     } catch (ClientProtocolException e) {
         // TODO Auto-generated catch block
     } catch (IOException e) {
         // TODO Auto-generated catch block
     }
      }
    else
    {Toast.makeText(getBaseContext(),"All field are                               required",Toast.LENGTH_SHORT).show();

    }
}

} enter code here

1 个答案:

答案 0 :(得分:0)

file_get_contents($filename)会返回值,因为你把它放在那里 您的default:中有一个switch Your favorite friend is neither