我一直在尝试将数据从android发送到本地服务器。我在成功发送数据时得到了Logcat的响应,但是我无法在网页上显示它。
Android代码是:
package rk.android.test.test;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import cz.msebera.android.httpclient.HttpEntity;
import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.NameValuePair;
import cz.msebera.android.httpclient.client.HttpClient;
import cz.msebera.android.httpclient.client.entity.UrlEncodedFormEntity;
import cz.msebera.android.httpclient.client.methods.HttpPost;
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;
import cz.msebera.android.httpclient.message.BasicNameValuePair;
import cz.msebera.android.httpclient.util.EntityUtils;
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.send);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DataAsyncTask().execute();
}
});
}
public class DataAsyncTask extends AsyncTask<String, String, String>
{
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected String doInBackground(String... params)
{
try {
String postUrl = "http://192.168.137.1/receiver/receiver.php";
HttpClient httpClient = new DefaultHttpClient();
// post header
HttpPost httpPost = new HttpPost(postUrl);
// add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("action", "Mohan"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// execute HTTP post request
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String responseStr = EntityUtils.toString(resEntity).trim();
Log.v("OP", "Response: " + responseStr);
// you can add an if statement here and do oth
}
}catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
}
php代码是:
<?php
$reversed = strrev($_POST["action"]);
echo $reversed;
?>
Logcat响应是:
04-06 02:36:55.789 17879-17959/rk.android.test.test V/OP: Response: nahoM
问题是我在加载receiver.php文件时得到一个空白页面!!
答案 0 :(得分:0)
如果这是您文件的唯一内容
echo strrev($_POST["action"]);
当然,导航到它时只会打印一个空白页面 - 因为$_POST['action']
将被取消设置。
如果您希望它显示某些内容,请将POST
更改为REQUEST
,然后导航到这样:
http://server:port/page.php?action=esreveRoTgnirtS
答案 1 :(得分:0)
receiver.php
脚本以反向&#34;动作响应&#34; POST主体中传递的参数。基于HttpClient
的代码提供此参数值。
当您尝试在网页中加载此网址时,请确保在POST正文中传递一些参数值。如果您不是,$reversed
将是一个空字符串,您将看到一个空白页。
为了在Web浏览器中通过POST传递参数,您需要使用XMLHttpRequest或<form>
元素,这意味着您需要一个额外的网页,其中包含<form action=".../receiver.php">
和<input name="action">
{ {1}}元素。
如果您使用_POST
或_GET
代替_REQUEST
(以处理这两种方法),则可以避免使用此方法。然后你可以通过&#34;动作&#34;参数通过URL的查询部分:&#34; .... / receiver.php?action = Mohan&#34;。