【问题标题】:Posting and parsing data from Android to php将数据从 Android 发布和解析到 php
【发布时间】:2012-10-07 04:52:38
【问题描述】:

我在我的 android 程序中将一些不同的数据收集到三个不同的变量中,这些变量具有不同的数据类型。

现在我需要将这些数据发布到服务器,在那里我应该能够解析这些数据并将它们存储在我的本地数据库中。我正在使用 php 编写服务器端脚本。

谁能给我一个例子如何使用httppost做到这一点?

【问题讨论】:

    标签: php android sql-server jakarta-ee


    【解决方案1】:

    向服务器发送请求并获取响应json是最好的实现方式。

    这是发送 httppost json 请求到服务器并处理 json 响应的一个很好的例子。

    http://www.codeproject.com/Articles/267023/Send-and-receive-json-between-android-and-php

    【讨论】:

    • 谢谢。我还有一个。如果我需要传递一个列表,我该怎么做?例如,列表 a 有 3 个值。所有这三个都应该使用相同的 ID。
    【解决方案2】:

    在 Android 端,您不应该这样做 network operations in the main UI Thread

    安卓端:

    public class SendPOSTRequest extends AsyncTask<List<BasicNameValuePair>, Void, String>
    {
        private DefaultHttpClient _httpClient;
        private String _url = "";
    
        public SendPOSTRequest(String url){
            _url = url;
            _httpClient = new DefaultHttpClient();
        }
    
        @Override
        protected String doInBackground(List<BasicNameValuePair>... postParameters) {
            String responseString = "";
    
            try
            {
                HttpPost postRequest = new HttpPost(_url);
                postRequest.setEntity(new UrlEncodedFormEntity(postParameters[0]));
    
                HttpResponse response = _httpClient.execute(postRequest);
                StatusLine statusLine = response.getStatusLine();
    
                // check if post was successfull
                if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
    
                    HttpEntity entity = response.getEntity();
                    entity.writeTo(out);
                    out.close();
                    responseString = out.toString();
    
                    if (entity != null) {
                        entity.consumeContent();
                    }
                }
            }
            catch(Exception ex)
            {
                ex.getMessage();
            }
    
            return responseString;
        }
    }
    

    在您的活动中,您可以像这样使用“SendPostRequest”-Class: SendPOSTRequest webPOSTRequest = new SendPOSTRequest(yourWebURLWithYourPHPFunction); 列出 postParams = new ArrayList(); postParams.add(new BasicNameValuePair("Name", "viperbone")); 字符串结果 = webGetRequestUsersEntries.execute(postParams).get();

    在服务器端,我使用了带有 PDO(PHP 数据对象)的 php-script,因为 it helpts to protect from sql injection

    服务器端 PHP 脚本:

    try
    {
        $DBH = new PDO("mysql:host=yourWebURL;dbname=yourDBName", username, password);
    
        # substr(str,pos,len) - Make sure POST-Data aren't too long (255 chars max) because my database-field is 255 chars
        $NameClear = substr($_POST['Name'], 0, 255);
    
        # named placeholders 
        $STH = $DBH->prepare("INSERT INTO `yourTableName` (Name) VALUES ( :name )");
        $STH->bindParam(':name', $NameClear);
    
        # setting the fetch mode
        $STH->setFetchMode(PDO::FETCH_ASSOC);
        $STH->execute(); 
    
        # I return 1 for a successful insertion 
        echo "1";
    
        $DBH = null;
    }  
    catch(PDOException $e) {
    } 
    

    希望对你有帮助……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多