从Android应用程序发送POST请求+ JSon到远程MySql

时间:2014-05-06 19:08:18

标签: java android mysql json

我尝试发送POST请求,将JSon发送到我的远程MySql数据库,但响应回答为"失败"。服务器端是正确的,我已经测试了她。以下是Android应用的源代码:

public class MainActivity extends Activity {

    public static final String TAG = MainActivity.class.getName();

    private TextView output;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        startService(new Intent(this.getApplicationContext(), OutSmsService.class));
        MessageSaver saver = new MessageSaver(getApplicationContext());
        saver.save();

        new Thread(new Runnable() {
            @Override
            public void run() {
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("from_col", "1111");
                    jsonObject.put("to_col", "2222");
                    jsonObject.put("body_col", "3333");
                    jsonObject.put("date_col", "444444");
                } catch (JSONException e) {
                    Log.d(TAG, e.getMessage());
                }
                Log.d("JSON", jsonObject.toString());

                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPostReq = new HttpPost("http://secret.com/test.php");
                StringEntity stringEntity = null;
                try {
                    stringEntity = new StringEntity(jsonObject.toString());
                } catch (UnsupportedEncodingException e) {
                    Log.d(TAG, e.getMessage());
                }
                if (stringEntity != null) {
                    stringEntity.setContentType("application/json;charset=UTF-8");
                    stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8"));
                }
                httpPostReq.setEntity(stringEntity);
                try {
                    HttpResponse httpResponse = httpClient.execute(httpPostReq);
                    HttpEntity entity = httpResponse.getEntity();
                    InputStream in = entity.getContent();
                    InputStreamReader reader = new InputStreamReader(in);
                    BufferedReader bufferedReader = new BufferedReader(reader);
                    String tmp = null;
                    while ((tmp = bufferedReader.readLine()) != null) {
                        Log.d(TAG, tmp);
                    }
                    bufferedReader.close();
                    reader.close();
                    in.close();
                } catch (IOException e) {
                    Log.d(TAG, e.getMessage());
                }
            }
        }).start();

    }


}

logcat的输出是("没有数据"表示服务器没有获取JSon数据):

05-06 18:03:18.535      768-784/com.wrk.app D/JSON﹕ {"from_col":"1111","to_col":"2222","date_col":"444444","body_col":"3333"}
05-06 18:03:18.695      768-768/com.wrk.app D/com.wrk.app.sms.MyService﹕ onStartCommand
05-06 18:03:18.745      768-768/com.wrk.app I/Choreographer﹕ Skipped 79 frames!  The application may be doing too much work on its main thread.
05-06 18:03:19.215      768-768/com.wrk.app D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
05-06 18:03:19.265      768-784/com.wrk.app D/com.wrk.app.MainActivity﹕ no data

服务器端:(test.php)

<?php 
require_once 'config.php';
if (isset($_GET['showform'])){
    ?>
    <!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="content-type" content="text/html" />

</head>

<body>
<form id="" name="" method="post" action="?showform">
    <input type="text" name="data[from_col]" value="from" />
    <input type="text" name="data[to_col]" value="to" />
    <input type="text" name="data[body_col]" value="body" />
    <input type="text" name="data[date_col]" value="<?php echo date('Y-m-d'); ?>" />
    <input type="submit" value="OK" />
</form>

</body>
</html>

    <?php
}
if (isset($_POST['data'])){
    $_POST['request'] = json_encode($_POST['data']);
    echo $_POST['request'];
}

if (isset($_POST['request']))
{
    mysql_connect($server,$login,$pass);
    mysql_selectdb($db_name);
    $data = json_decode($_POST['request'],true);
    $from_col   = mysql_real_escape_string(trim(($data['from_col'])));
    $to_col     = mysql_real_escape_string(trim(($data['to_col'])));
    $body_col   = mysql_real_escape_string(trim(($data['body_col'])));
    $date_col   = mysql_real_escape_string(trim(($data['date_col'])));
    $q = "insert into sms_db (from_col,to_col,body_col,date_col) values ('$from_col','$to_col','$body_col','$date_col')";
    if (@mysql_query($q)) echo 'success';
    else echo 'error';
}
else {
    echo 'no data';
}

?>

0 个答案:

没有答案