chongman
chongman

Reputation: 13

HTTP "POST" in Java

I want to use httpPost in java(android) to post some data to a php page, but some wrong happened.

Java code:

btn1.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View arg0) {
        HttpPost urlPost = new HttpPost("http://192.168.1.107/update.php");
        List<NameValuePair> parms = new ArrayList<NameValuePair>();
        parms.add(new BasicNameValuePair("a","heyya"));
        try{
            urlPost.setEntity(new UrlEncodedFormEntity(parms,HTTP.UTF_8));
            Log.i("aaa","1");
            HttpResponse postResponse = new DefaultHttpClient().execute(urlPost);
            Log.i("aaa","2");
            Log.i("aaa",Integer.toString(postResponse.getStatusLine().getStatusCode()));
        }catch(Exception e){ }
    }
});

PHP code:

<?php
$inputVal = $_POST["a"];
if($inputVal != ""){
    $mysql_address = "localhost";
    $mysql_user = "root";
    $mysql_pw = "123123";
    $mysql_db = "test";
    $conn = mysql_connect($mysql_address,$mysql_user,$mysql_pw);
    mysql_select_db($mysql_db);
    $sql = 'insert into test(a) VALUES("'.$inputVal.'")';
    mysql_query($sql);
}else{
    echo 'Error!';
}
?>

Now the LogCat just appear "1" and means that the program blocked at

"new DefaultHttpClient().execute(urlPost);"

What's wrong with these code?

Thank you in advance.

Upvotes: 0

Views: 156

Answers (1)

Eugene
Eugene

Reputation: 120858

Probably some exception was thrown, but you swallow it silently, add this :

 catch(Exception e){ e.printStackTrace(); } // or LOG.error if you have it..

and try again.

Upvotes: 3

Related Questions