2016-01-22 34 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