![](/img/trans.png)
[英]How to send request(String data) from android to PHP server localhost and receive response result form server PHP
[英]Send Data from android to server (Localhost)
我一直在嘗試將數據從 android 發送到本地服務器。我在成功發送數據時在 Logcat 上得到響應,但我無法在網頁上顯示它。
安卓代碼是:
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">
的額外網頁<form action=".../receiver.php">
和適當的<input name="action">
元素。
如果您使用_GET
或_REQUEST
(處理這兩種方法)而不是_POST
則可以避免這種需要。 然后你可以通過 URL 的查詢部分傳遞“action”參數:“..../receiver.php?action=Mohan”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.