-1

I am trying to upload a Base64 String using Post method of HTTP and this is my code:

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();



        }

    }

When I try to use the GET method, I get an error of Request URI too long. So I commented that out. But when I shifted to POST method, I think it was never executed since I couldn't see the TAG RESPONSE SIGNATURE.

I don't have any clue what is wrong.

UPDATED PHP CODE:

  <?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());
}

?>
Jayson Tamayo
  • 2,741
  • 3
  • 49
  • 76
  • Possible duplicate of [How to do a HTTP Post in Android?](http://stackoverflow.com/questions/4470936/how-to-do-a-http-post-in-android) – Kevin Kopf Jan 29 '16 at 16:03
  • I certainly know how to do HTTP POST in Android. But this case is different – Jayson Tamayo Jan 29 '16 at 16:06
  • You have to change your php code to use $_POST. And where is your base64 string? In Android and in php please. – greenapps Jan 29 '16 at 16:15
  • `don't have any clue what is wrong.`. Indeed. Your php script is not echoing info. And you have no Log statements in the catch blocks so you dont know how your code flows. And you probably dont look in the LogCat. – greenapps Jan 29 '16 at 16:19
  • @greenapps I added Log statements in every catch. Im sure there's nothing in the LogCat. – Jayson Tamayo Jan 29 '16 at 16:33

1 Answers1

0

In android code, you are posting parameters by POST method and in php you are trying to retrieve it using GET method. I think that's the error.

Replace $_GET in php to $_POST and it will work.

ELITE
  • 5,815
  • 3
  • 19
  • 29
  • I tried changing GET to POST, please see the updated code above, but still not working. I added Log.e in every catch but Im sure there's nothing in the LogCat. – Jayson Tamayo Jan 29 '16 at 16:31
  • You're comparing action `if($action == "get_num_rows"){` like this and sending action from android code `("action", "add_signature")` like this... – ELITE Jan 29 '16 at 16:39