簡體   English   中英

通過Post Android將數據發送到PHP

[英]Send Data Via Post Android to PHP

因此,我正在嘗試通過Android中的Post將消息發送到PHP,這是Android Java函數:

 //enviando para o backend
private void SendtoPHP(String reg) throws IOException {


    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://bubbledev.com.br/gcm/getdevice.php");

    try{
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("regid", "" + reg ));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);


    }
    catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

}

PHP:

<?php       
include 'conecta.php';
$device_token  = urldecode($_POST['regid']);    
$sql = "INSERT INTO corposa.deviceandroid"
              . " (id,device_token)"
              . " VALUES (NULL,'$device_token')";
mysqli_query($con,$sql);    

?>

PHP很好,我已經在另一個Post上對其進行了測試並可以工作,但是當Android函數嘗試$ device_token時,不要接收任何值,並且SQL在表中保存帶有ID的“”

除了ResponseHandler之外,我使用與您的代碼相似的代碼。 這個對我有用。

HttpClient Client = new DefaultHttpClient();

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", sID));
nameValuePairs.add(new BasicNameValuePair("etc", sETC));

try {           

    String SetServerString = "";

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://your-url.com/script.php");
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    ResponseHandler<String> responseHandler = new BasicResponseHandler();

    SetServerString = httpclient.execute(httppost, responseHandler);                

}  catch(Exception ex) {
    // failed
}

我敢肯定有更好的方法可以做到這一點,但這是我通常處理來自Android的發布請求的方式:

<?php
{
    $input = json_encode(file_get_contents("php://input"));

    $deviceToken = $input->regId;
    mysqli_query($connection, "INSERT INTO corposa.deviceandroid (`id`, `device_token`) VALUES(NULL, '" . mysqli_real_escape_string($deviceToken) . "')";
}

另外,由於您使用的是POST而不是GET,因此傳遞到服務器的數據不是經過url編碼的,因此這里不需要urldecode。

代碼似乎還可以,只有“奇怪”的東西是:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

因為您僅將一個BasicNameValuePair添加到列表中。 也許是問題所在...?

我在當前項目中使用了此代碼,希望它也對您有用

  Map<String, String> kvPairs = new HashMap<String, String>();
    kvPairs.put("regid", reg);
    HttpClient httpclient = this.getNewHttpClient();
    HttpPost httppost = new HttpPost("http://bubbledev.com.br/gcm/getdevice.php");
    if (kvPairs != null && kvPairs.isEmpty() == false) {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(kvPairs.size());
        String k, v;
        Iterator<String> itKeys = kvPairs.keySet().iterator();
        while (itKeys.hasNext()) {
            k = itKeys.next();
            v = kvPairs.get(k);
            nameValuePairs.add(new BasicNameValuePair(k, v));
        }
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
    }
    HttpResponse response;
    response = httpclient.execute(httppost);
    String responseString = EntityUtils.toString(response.getEntity());

您的課程中也需要此方法

public static HttpClient getNewHttpClient() {
    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new RecorridoSSL(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}

希望我不要忘記任何事情,以其他方式告訴我

伙計們,錯誤是我的PHP

$device_token  = urldecode($_POST['regid']); 

這個urldecode弄亂了我的代碼哈哈

感謝所有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM