HTTP Post未执行

时间:2016-01-29 15:56:38

标签: php android

我正在尝试使用HTTP的Post方法上传Base64字符串,这是我的代码:

public void upload_signature(){
        final DBHelper db = new DBHelper(et);
        ArrayList<HashMap<String, String>> array_list = new ArrayList<HashMap<String, String>>();
        array_list = db.getSignatures(ticket);
        Log.i("Num Sig", "" + array_list.size());
        for(int i = 0; i < array_list.size(); i++){
            final String chaka = array_list.get(i).get("signature");

            final SharedPreferences prefs = EnterTotals.this.getSharedPreferences("-", MODE_PRIVATE);
            /**
            HttpClient Client = new DefaultHttpClient();
            String URL = "http://hs.ha.com/api/RetrieveDispatch.php?action=add_signature="
                    + "&hs_customer=" + prefs.getInt("hs_customer", 1) + "&ticket=" + ticket + "&signature_path=" + chaka;
            HttpGet httpget = new HttpGet(URL);
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            if(isAvailable()) {
                try {
                    String result = Client.execute(httpget, responseHandler);
                    Log.i("info", result + " " + URL);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            */
            Thread thread = new Thread(new Runnable(){
                @Override
                public void run() {
                    HttpParams httpParams = new BasicHttpParams();
                    HttpConnectionParams.setConnectionTimeout(httpParams, 60000);  //1 min
                    HttpConnectionParams.setSoTimeout(httpParams, 60000);          //1 min

                    HttpClient client = new DefaultHttpClient(httpParams);
                    HttpPost post = new HttpPost("http://hs.ha.com/api/RetrieveDispatch.php");

                    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
                    pairs.add(new BasicNameValuePair("action", "add_signature"));
                    pairs.add(new BasicNameValuePair("signature_path", chaka));
                    pairs.add(new BasicNameValuePair("hs_customer", "" + prefs.getInt("hs_customer", 1)));
                    pairs.add(new BasicNameValuePair("ticket", "" + ticket));

                    try {
                        post.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8));   // as UTF-8
                        HttpResponse response = client.execute(post);
                        HttpEntity ent = response.getEntity();
                        String text = EntityUtils.toString(ent);
                        Log.i("RESPONSE SIGNATURE: ", "" + text);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    } catch (ClientProtocolException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
            thread.start();



        }

    }

当我尝试使用GET方法时,我得到的Request URI错误太长。所以我评论说出来。但是当我转向POST方法时,我认为它从未执行过,因为我看不到TAG RESPONSE SIGNATURE。

我不知道出了什么问题。

更新的PHP代码:

  <?php
require_once("../includes/common.php");

$action = trim($_POST['action']);

if($action == "get_num_rows"){
$hs_customer = trim($_POST['hs_customer']);
    $ticket = trim($_POST['ticket']);
    $signature_path = trim($_POST['signature_path']);

    $sql = "INSERT INTO jsa_signatures SET hs_customer = '$hs_customer', ticket = '$ticket', signature_path = '$signature_path'";

    mysql_query($sql) or die(mysql_error());
}

?>

1 个答案:

答案 0 :(得分:0)

在Android代码中,您通过POST方法发布参数,在php中,您尝试使用GET方法检索它。我认为这是错误。

将php中的$_GET替换为$_POST,它会起作用。