为什么我的android应用程序不是';在POST方法之后是否没有响应?

为什么我的android应用程序不是';在POST方法之后是否没有响应?,android,node.js,json,post,httpurlconnection,Android,Node.js,Json,Post,Httpurlconnection,我有个问题。我正在尝试对Node.js服务器执行POST方法。使用POST方法后,我将获取服务器中的所有数据,但我的应用程序几秒钟内没有响应。我的代码中有错误吗 我的发帖方法: public static void setTemp(String address, String hot, String cold) throws IOException { URL url = new URL(address); //in the real code, there is an

我有个问题。我正在尝试对Node.js服务器执行POST方法。使用POST方法后,我将获取服务器中的所有数据,但我的应用程序几秒钟内没有响应。我的代码中有错误吗

我的发帖方法:

public static void setTemp(String address, String hot, String cold) throws IOException
    {
        URL url = new URL(address); //in the real code, there is an ip and a port
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        try {
            conn.connect();

            JSONObject jsonParam = new JSONObject();
            jsonParam.put("hot", hot);
            jsonParam.put("cold", cold);

            DataOutputStream os = new DataOutputStream(conn.getOutputStream());
            os.writeBytes(jsonParam.toString());

            os.flush();
            os.close();

            Log.i("STATUS", String.valueOf(conn.getResponseCode()));
            Log.i("MSG" , conn.getResponseMessage());

        } catch (Exception e) {

        }
        finally {
            conn.disconnect();
        }
    }
    private void setTemp(String hot, String cold)
    {
        try {
            WebAPI.setTemp(Tools.RestURLPost, hot, cold);
        }
        catch(IOException e) {
            e.printStackTrace();
        }
    }
这是我如何调用POST方法的:

public static void setTemp(String address, String hot, String cold) throws IOException
    {
        URL url = new URL(address); //in the real code, there is an ip and a port
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        try {
            conn.connect();

            JSONObject jsonParam = new JSONObject();
            jsonParam.put("hot", hot);
            jsonParam.put("cold", cold);

            DataOutputStream os = new DataOutputStream(conn.getOutputStream());
            os.writeBytes(jsonParam.toString());

            os.flush();
            os.close();

            Log.i("STATUS", String.valueOf(conn.getResponseCode()));
            Log.i("MSG" , conn.getResponseMessage());

        } catch (Exception e) {

        }
        finally {
            conn.disconnect();
        }
    }
    private void setTemp(String hot, String cold)
    {
        try {
            WebAPI.setTemp(Tools.RestURLPost, hot, cold);
        }
        catch(IOException e) {
            e.printStackTrace();
        }
    }
在这里您可以找到我的Node.js方法,我用它来测试JSON的成功解析:

router.post('/post', function(req, res, next) {
  console.log(req.body);
});

如果看不到完整的代码,很难知道,但您永远不会在Node中结束请求,因此请使用:
req.send/json
,否则Android应用程序将等待请求完成,这不会发生,并且会超时

router.post('/post', function(req, res, next) {
  console.log(req.body);
  res.json({ success: true });
});

我想你是在后台运行,对吗?我不知道是否可以在主线程中运行网络请求,但我使用Android SDK编写任何代码已经有几年了。我们的node.js路由不发送任何响应,因此客户端代码仍然坐在那里等待响应。至少将
res.send(“ok”)
res.json({status:“ok”})
添加到node.js路由。当捕获错误时,您可能还希望在Android应用程序中输出或记录错误。