【问题标题】:JSON data sent from Android does not show on the server从 Android 发送的 JSON 数据未显示在服务器上
【发布时间】:2015-11-16 04:51:09
【问题描述】:

我正在尝试将 JSON 数据从 android 应用程序发送到 php 服务器。我只是要求服务器打印原始数据,但它没有显示在网络上。我无法弄清楚可能是什么原因。 DisplayMessageActivity.java:

public class DisplayMessageActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);
    new SaveTheFeed().execute();
}

class SaveTheFeed extends AsyncTask<Void, Void, Void> {

    @Override
    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);
        // Create the JSONObject
        JSONObject request = CreateRequest(message, longitude, latitude);
        //JSONObject response = null;
        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.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            client.setRequestMethod("POST");
            client.connect();
            Log.d("doInBackground(Request)", request.toString());
            // Send the JSON object to the server
            OutputStreamWriter writer = new OutputStreamWriter(client.getOutputStream());
            String output = request.toString();
            writer.write(output);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            client.disconnect();
        }
        return null;
    }

    private JSONObject CreateRequest(String message, double longitude, double latitude) {
        JSONObject request = new JSONObject();
        try {
            request.put("message", message);
            request.put("longitude", longitude);
            request.put("latitude", latitude);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return request;
    }
}
}

php代码:

<!DOCTYPE html>
<html>
<body>
<h2>This is your current location :)
<?php
      $json = file_get_contents('php://input');
      $request = json_decode($json, true);
      echo ($json);
?>
</h2>
<div id="map"></div>
</body>
</html>

【问题讨论】:

  • 或者我怎么知道模拟器是否真的将数据发送到url?现在我不确定哪一方有问题。
  • 通过client.getResponseCode()从客户端检查respose代码,可以追踪问题
  • 为什么要将json对象的String发送到服务器?参考这个androidexample.com/…
  • @San 我检查了响应代码,它是 200。我认为这意味着连接成功?但它仍然不会向服务器发布消息。

标签: php android json


【解决方案1】:

我认为您在代码中遗漏的是使用 URLEncoder 对字符串进行编码。而不是只写原始字符串在写之前对字符串进行编码。尝试替换此行:

writer.write(output)

用这条线

writer.write(URLEncoder.encode(output,"UTF-8");

让我知道输出。 也可以参考stackoverflow中的this link

更新:

也不要忘记在您的 logcat 窗口中记录服务器响应。为此,只需添加以下代码行:

        String line="";
        BufferedReader reader=new BufferedReader(new InputStreamReader(conn.getInputStream()));
        while ((line=reader.readLine())!=null){
            response+=line;
        }
        Log.d("response from server",response);
        reader.close();

【讨论】:

  • 嗨!我按照您的建议更换了线路。但是应用程序在执行时停止了。我不认为它有效。 :(
  • 我需要修改我的php文件吗?
  • 你能提供logcat的输出吗?如果你发布你的 logcat 输出会更容易
  • 还记得是否应该在client.close()之前添加上面的代码片段;即在 writer.close() 语句之后
猜你喜欢
  • 2017-06-12
  • 1970-01-01
  • 1970-01-01
  • 2012-01-09
  • 2016-07-13
  • 1970-01-01
  • 1970-01-01
  • 2011-09-25
  • 1970-01-01
相关资源
最近更新 更多