从Android发送的JSON数据未显示在服务器上 [英] JSON data sent from Android does not show on the server

查看:105
本文介绍了从Android发送的JSON数据未显示在服务器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to send a JSON data from an android app to a php server. I simply asked the server to print the raw data but it is not shown on the web. I cannot figure out what might be the cause. 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>

推荐答案

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

I think what you missed in your code is to encode your string using URLEncoder. Instead of just writing the raw string encode the string before writing it. Try replacing this line:

writer.write(output)

与此行

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

,让我知道输出. 您还可以在stackoverflow

and let me know the output. You can also refer to this link in stackoverflow

更新:

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

Also don't forget to log the server response in your logcat window. To do that simply add the following lines of code:

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

这篇关于从Android发送的JSON数据未显示在服务器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆