将数据从android发送到服务器(Localhost)


Send Data from android to server (Localhost)

我一直在尝试将数据从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文件时得到了一个空白页面!!

如果这是文件的唯一内容

echo strrev($_POST["action"]);

当然,当导航到它时,它只会打印一个空白页,因为$_POST['action']将被取消设置。

如果你想让它显示一些东西,把POST改为REQUEST,然后像这样导航到它:

http://server:port/page.php?action=esreveRoTgnirtS

receiver.php脚本使用POST主体中传递的反向"action"参数进行响应。基于HttpClient的代码提供了此参数值。

当您尝试在网页中加载此URL时,请确保在POST正文中传递一些参数值。如果不这样做,$reversed将是一个空字符串,您将看到一个空白页面。

为了在web浏览器中通过POST传递参数,您需要使用XMLHttpRequest或<form>元素,这意味着您需要一个额外的网页,该网页将具有<form action=".../receiver.php">和适当的<input name="action">元素。

如果使用_GET_REQUEST(处理这两种方法)而不是_POST,则可以避免这种需要。然后,您可以通过URL的查询部分传递"action"参数:"…./receiver.php?action=Mohan".