// 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
答案 0 :(得分:0)
file_get_contents($filename)
会返回值,因为你把它放在那里
您的default:
中有一个switch
Your favorite friend is neither