2016-01-22 36 views
0

我想將一些數據發佈到一個URL,然後從該URL獲取發佈的數據並將其存儲在數據庫中。我試圖迴應發佈的參數,但它們似乎沒有發佈,也沒有顯示任何錯誤。 下面是我用做後期功能:使用Android Volley向URL發佈數據不起作用

public void insertDataWithVolley() { 
     String url = "http://apps.example.com/application/index.php/main/getParamsFromVolley"; 
     RequestQueue requestQueue = Volley.newRequestQueue(this); 
     StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { 
      @Override 
      public void onResponse(String response) { 
       Toast.makeText(getApplicationContext(), response, 
         Toast.LENGTH_LONG).show(); 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Toast.makeText(getApplicationContext(), error.getMessage(), 
         Toast.LENGTH_LONG).show(); 

      } 
     }) { 
      protected Map<String, String> getParams() throws AuthFailureError { 
       Map<String, String> mapObject = new HashMap<>(); 
       mapObject.put("id", "ID"); 
       Log.d("TAG", "Put id"); 
       mapObject.put("name", "NAME"); 
       Log.d("TAG", "Put name"); 
       return mapObject; 
      } 
     }; 
     requestQueue.add(stringRequest); 
    } 

在PHP端,這是功能,見下圖:

public function getParamsFromVolley() { 
     $id = null; 
     $name = null; 

     if (isset($_GET['id'])) { 
      $id= $this->input->get('id'); 
     } 
     if (isset($_GET['name'])) { 
      $name= $this->input->get('name'); 
     } 
     //store $pesapal_tracking_id in your database against the order with orderid = $reference09 
     echo 'name>>>>' . $id; 
     echo 'email>>>>' . $name;    

     //return $pesapal_tracking_id; 
    } 

請協助

+0

你的網址看起來有點懷疑 - (http://apps.example.com/application/index.php/main/getParamsFromVolley) – Tasos

+0

嗨,我可以訪問除了獲取網址的其他東西。我編輯了這個問題,以包含我打電話的服務器端功能。 – Kabs

+0

這將有助於您發佈URL – Sharj

回答

0

我不知道PHP,但您在使用Volley進行POST請求時檢查PHP中的GET。更改PHP代碼以檢查POST參數。

if (isset($_POST['id'])) { 
     $id= $this->input->get('id'); 
    } 
    if (isset($_POST['name'])) { 
     $name= $this->input->get('name'); 
    } 

也做一個簡單的html頁面來檢查您的發佈請求是否在更改後工作。

+0

嗨,即使編輯到POST後,它不會帶來所需的結果 – Kabs

+0

@Kabs - 嘗試並回顯一個結果並在另一端分割它 - 例如(echo'name >>>>'。$ id。#'email >>>>'。$ name;)並分割爲#字符。其他的事情你可以嘗試的是尾巴你的PHP/Apache的錯誤日誌文件,看看你是否得到任何錯誤與你的PHP腳本,當你提出排除請求 – Tasos

+0

@Kabs正如我上面所說,問題是與您的PHP代碼。首先請確保您在Android應用程序中嘗試之前處理POST請求。您可以使用示例HTML表單嘗試使Post請求正確。 – Sharj