2016-04-05 164 views
-1

我一直在嘗試從android發送數據到本地服務器。我在Logcat上獲得了成功發送數據的響應,但我無法在網頁上顯示它。發送數據從android到服務器(本地主機)

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

如果這是

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

那麼當然它會導航到它時,僅打印空白頁文件的唯一內容 - 因爲$_POST['action']將取消設置。

如果你想這顯示的東西,改變POSTREQUEST代替,然後導航到它是這樣的:

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

receiver.php腳本在POST體通過顛倒「action」參數進行響應。基於HttpClient的代碼提供了此參數值。

當您嘗試在網頁中加載此URL時,請確保您在POST正文中傳遞了一些參數值。如果你沒有,$reversed將是一個空字符串,你會看到一個空白頁。

爲了通過POST在Web瀏覽器來傳遞參數,你需要或者使用XMLHttpRequest,或<form>元素,這意味着你將需要一個額外的網頁,將有一個<form action=".../receiver.php">和適當<input name="action">元素。

如果您不使用_POST而使用_GET_REQUEST(以處理這兩種方法),則可避免使用此功能。然後你可以通過URL的查詢部分傳遞「action」參數:「..../receiver.php?action = Mohan」。

相關問題