Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从Android发送的JSON数据不会显示在服务器上_Php_Android_Json - Fatal编程技术网

Php 从Android发送的JSON数据不会显示在服务器上

Php 从Android发送的JSON数据不会显示在服务器上,php,android,json,Php,Android,Json,我正在尝试从android应用程序向php服务器发送JSON数据。我只是要求服务器打印原始数据,但它没有显示在web上。我想不出原因是什么。 DisplayMessageActivity.java: public class DisplayMessageActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(sav

我正在尝试从android应用程序向php服务器发送JSON数据。我只是要求服务器打印原始数据,但它没有显示在web上。我想不出原因是什么。 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;
    }
}
}
public类DisplayMessageActivity扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u display\u message);
新建savefeed().execute();
}
类savefeed扩展异步任务{
@凌驾
受保护的Void doInBackground(Void…参数){
//从意图中获得信息
Intent=getIntent();
String message=intent.getStringExtra(MapsActivity.EXTRA_message);
双经度=intent.getDoubleExtra(MapsActivity.EXTRA_经度,0.0);
双纬度=intent.getDoubleExtrade(MapsActivity.EXTRA_纬度,0.0);
//创建JSONObject
JSONObject request=CreateRequest(消息、经度、纬度);
//JSONObject响应=null;
URL=null;
HttpURLConnection客户端=null;
试一试{
//建立http连接
url=新url(“http://*********.com/”;
client=(HttpURLConnection)url.openConnection();
client.setDoOutput(true);
client.setDoInput(true);
setRequestProperty(“内容类型”,“应用程序/json;字符集=UTF-8”);
客户端。setRequestMethod(“POST”);
client.connect();
Log.d(“doInBackground(Request)”,Request.toString();
//将JSON对象发送到服务器
OutputStreamWriter writer=新的OutputStreamWriter(client.getOutputStream());
字符串输出=request.toString();
writer.write(输出);
writer.flush();
writer.close();
}捕获(IOE异常){
e、 printStackTrace();
}最后{
client.disconnect();
}
返回null;
}
私有JSONObject CreateRequest(字符串消息、双经度、双纬度){
JSONObject请求=新建JSONObject();
试一试{
请求。放置(“消息”,消息);
请求。放置(“经度”,经度);
请求。输入(“纬度”,纬度);
}捕获(JSONException e){
e、 printStackTrace();
}
返回请求;
}
}
}
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>

这是您当前的位置:)

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

writer.write(output)
用这条线

writer.write(URLEncoder.encode(output,"UTF-8");
并让我知道输出。 您还可以参考stackoverflow中的

更新:

另外,不要忘记在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();

或者我怎么知道模拟器是否真的将数据发送到url?现在我不确定是哪一方出了问题。逐个client.getResponseCode()检查客户端的响应代码,就可以跟踪问题了为什么要向服务器发送json对象的字符串?参考此@San,我检查了响应代码,它是200。我想这意味着连接成功了?但它仍然没有将消息发布到服务器。嗨!我按照你的建议换了线。但应用程序在执行时停止。我认为它不起作用(我需要修改我的php文件吗?你能提供logcat输出吗?如果你发布你的logcat输出会更容易。记住你是否应该在client.close()之前,即writer.close()语句之后添加上面的代码段。)