从Android应用程序发送到服务器的Http不起作用

时间:2015-11-17 02:46:39

标签: android http-post

我有一个Android应用程序,它将http帖子发送到远程服务器:

 protected Void doInBackground(Void... params) {
        // Get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MapsActivity.EXTRA_MESSAGE);
        double longitude = intent.getDoubleExtra(MapsActivity.EXTRA_LONGITUDE, 0.0);
        double latitude = intent.getDoubleExtra(MapsActivity.EXTRA_LATITUDE, 0.0);
        Log.d("doInBackground", message);
        Log.d("doInBackground", String.valueOf(longitude));
        Log.d("doInBackground", String.valueOf(latitude));
        URL url = null;
        HttpURLConnection client = null;
        try {
            // Establish http connection
            url = new URL("http://******.com/");
            client = (HttpURLConnection) url.openConnection();
            client.setDoOutput(true);
            client.setDoInput(true);
            client.setRequestMethod("POST");
            client.connect();
            OutputStreamWriter writer = new OutputStreamWriter(client.getOutputStream());
            String output;
            output = URLEncoder.encode("message", "UTF-8")
                    + "=" + URLEncoder.encode(message, "UTF-8");

            output += "&" + URLEncoder.encode("longitude", "UTF-8") + "="
                    + URLEncoder.encode(String.valueOf(longitude), "UTF-8");

            output += "&" + URLEncoder.encode("latitude", "UTF-8") + "="
                    + URLEncoder.encode(String.valueOf(latitude), "UTF-8");
            Log.d("doInBackground(output)", output);
            Log.d("doInBackground(code)", String.valueOf(client.getResponseCode()));  // Return 200
            writer.write(output);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            client.disconnect();
        }
        return null;
    }

在服务器中,我有:

    <?php
      $m = urldecode($_POST['message']);
      $long = urldecode($_POST['longitude']);
      $lat = urldecode($_POST['latitude']);
      print " ==== POST DATA =====
      Message  : $m
      Longitude : $long
      Latitude  : $lat"; 
?>

client.getResponseCode()返回200,我认为这意味着我的连接成功了吗?但网站仍然没有显示任何内容。什么可能导致问题? 我得到了

E/GMPM: getGoogleAppId failed with status: 10
E/GMPM: Uploading is not possible. App measurement disabled

这可能是问题吗?

1 个答案:

答案 0 :(得分:0)

你的意思是网站没有显示任何内容?重新加载网站时无法看到打印,因为您没有将其保存在任何地方,只需在单个请求上打印出值即可。要调试你可以将post params写入文件,而不是看它们是通过还是更好,但是在android端登录返回的对象。