Android web视图未将post数组发送到php


Android web view not sending post array to php?

问题是我没有从android webview收到任何$_POST['registerationID']我在android中有这样的代码java::

@Override
protected void onRegistered(Context context, String registrationId) {
    String URL_STRING = "http://mysite.org/mysite/index.php/user/notification/";
    Log.i(MyTAG, "onRegistered: registrationId=" + registrationId);
    // notification 
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("registrationId",registrationId));
    try{
        HttpPost httppost = new HttpPost(URL_STRING);
        httppost.setHeader("Content-Type","text/plain");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setBooleanParameter("http.protocol.expect-continue", false);
        HttpResponse response = httpclient.execute(httppost);
        Log.i("LinkPOST:", httppost.toString());
        Log.i("postData", response.getStatusLine().toString());
        HttpEntity httpEntity = response.getEntity();
        if (httpEntity != null){
            //System.out.println("Not Empty");
            String responseBody = EntityUtils.toString(httpEntity);
            System.out.println(responseBody);
        } else {
            System.out.println("Empty");
        }
    }
    catch(Exception e)
    {
        Log.e("log_tag", "Error in http connection "+e.toString());
    } 
}

我在php中处理httprequest帖子(使用codeigniter)如下:

function notification() {
                $registrationId = $_POST['registrationId'];
                if($this->session->userdata('emailid')) {
                //echo 'working from inside the if statement'.$this->session->userdata('emailid');
                //$query = $this->db->query('INSERT INTO user (`deviceid`) VALUES ('.$_GET['registerationID'].') where `emailid`='.$this->session->userdata('emailid').';');
                $data = array(
                               'deviceid' => $registrationId,
                            );
                $this->db->where('emailid', $this->session->userdata('emailid'));
                $this->db->update('user', $data);
                if($this->db->affected_rows() == 1)  {
                              // some code
                    }
                else {
                              // some code
                    }
            }

尝试使用下面的函数。它对我有用。

只需为您的post-params 填充哈希图

private static void post(String url, Map<String, String> params)
        throws IOException {   
    URL url;
    try {
        url = new URL(endpoint);
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException("invalid url: " + endpoint);
    }
    StringBuilder bodyBuilder = new StringBuilder();
    Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
    // constructs the POST body using the parameters
    while (iterator.hasNext()) {
        Entry<String, String> param = iterator.next();
        bodyBuilder.append(param.getKey()).append('=')
                .append(param.getValue());
        if (iterator.hasNext()) {
            bodyBuilder.append('&');
        }
    }
    String body = bodyBuilder.toString();
    Log.v(TAG, "Posting '" + body + "' to " + url);
    byte[] bytes = body.getBytes();
    HttpURLConnection conn = null;
    try {
        Log.e("URL", "> " + url);
        conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setFixedLengthStreamingMode(bytes.length);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded;charset=UTF-8");
        // post the request
        OutputStream out = conn.getOutputStream();
        out.write(bytes);
        out.close();
        // handle the response
        int status = conn.getResponseCode();
        if (status != 200) {
          throw new IOException("Post failed with error code " + status);
        }
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
  }

尝试更换

httppost.setHeader("Content-Type","text/plain") 

通过

httppost.setHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8")

这是php代码所期望的。

有关更多详细信息,请参阅此SO问题