最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

从android到firebase云功能的POST请求

IT培训 admin 5浏览 0评论

从android到firebase云功能的POST请求

我试图分别调用我的firebase云功能但是一切似乎都运行良好,除了json参数无法访问request.body.PARAMETER_NAME总是返回undefined并且在这一点上卡住了。

这是我的android代码:

try {
                      JSONObject jsonParam = new JSONObject();
                      jsonParam.put("param1", "v1");
                      jsonParam.put("param2", 2302355);
                      jsonParam.put("param3", "v2");

                      URL url = new URL(urls[0]);
                      HttpURLConnection connection;
                      connection = (HttpURLConnection) url.openConnection();
                      connection.setRequestProperty("Authorization", idToken);
                      connection.setRequestMethod("POST");
                      connection.setRequestProperty("Content-Type", "application/json");
                      connection.setUseCaches(false);
                      connection.setAllowUserInteraction(false);
                      connection.setConnectTimeout(10000);
                      connection.setReadTimeout(10000);
                      //Write
                      OutputStream outputStream = connection.getOutputStream();
                      BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                      writer.write(jsonParam.toString());
                      writer.close();
                      outputStream.close();
                      ///
                      connection.connect();
                      int res = connection.getResponseCode();
                      a = (res == HttpURLConnection.HTTP_OK);
                      if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                        server_response = readStream(connection.getInputStream());
                      }
                    } catch (IOException e) {
                      e.printStackTrace();
                    }
                    //return (a) ? server_response : "Nothing";
                    return server_response;

我的服务器代码:

exports.SOME_FUNCTION_NAME = functions.https.onRequest(function (request, response) {
if (!request.headers.authorization) {
    console.error('No Firebase ID token was passed');
    response.status(403).send('Unauthorized');
    return;
}
admin.auth().verifyIdToken(request.headers.authorization).then(function (decodedIdToken) {
    request.user = decodedIdToken;
    response.send(request.body + '\n'+request.params+'\n'+request.data+'\n'+request.headers);
}).catch(function (error) {
    console.error('Error while verifying Firebase ID token:', error);
    response.status(403).send('Unauthorized ' + error);
});

});

这是我一直得到的回应:

[object Object][object Object][undefined][object Object]

对于request.body.toJSON,我得到了undefinedas的回应

可能有什么不对?

提前致谢。

回答如下:

我发现使用request.body.PARAM_NAME可以正常工作。

从android到firebase云功能的POST请求

我试图分别调用我的firebase云功能但是一切似乎都运行良好,除了json参数无法访问request.body.PARAMETER_NAME总是返回undefined并且在这一点上卡住了。

这是我的android代码:

try {
                      JSONObject jsonParam = new JSONObject();
                      jsonParam.put("param1", "v1");
                      jsonParam.put("param2", 2302355);
                      jsonParam.put("param3", "v2");

                      URL url = new URL(urls[0]);
                      HttpURLConnection connection;
                      connection = (HttpURLConnection) url.openConnection();
                      connection.setRequestProperty("Authorization", idToken);
                      connection.setRequestMethod("POST");
                      connection.setRequestProperty("Content-Type", "application/json");
                      connection.setUseCaches(false);
                      connection.setAllowUserInteraction(false);
                      connection.setConnectTimeout(10000);
                      connection.setReadTimeout(10000);
                      //Write
                      OutputStream outputStream = connection.getOutputStream();
                      BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                      writer.write(jsonParam.toString());
                      writer.close();
                      outputStream.close();
                      ///
                      connection.connect();
                      int res = connection.getResponseCode();
                      a = (res == HttpURLConnection.HTTP_OK);
                      if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                        server_response = readStream(connection.getInputStream());
                      }
                    } catch (IOException e) {
                      e.printStackTrace();
                    }
                    //return (a) ? server_response : "Nothing";
                    return server_response;

我的服务器代码:

exports.SOME_FUNCTION_NAME = functions.https.onRequest(function (request, response) {
if (!request.headers.authorization) {
    console.error('No Firebase ID token was passed');
    response.status(403).send('Unauthorized');
    return;
}
admin.auth().verifyIdToken(request.headers.authorization).then(function (decodedIdToken) {
    request.user = decodedIdToken;
    response.send(request.body + '\n'+request.params+'\n'+request.data+'\n'+request.headers);
}).catch(function (error) {
    console.error('Error while verifying Firebase ID token:', error);
    response.status(403).send('Unauthorized ' + error);
});

});

这是我一直得到的回应:

[object Object][object Object][undefined][object Object]

对于request.body.toJSON,我得到了undefinedas的回应

可能有什么不对?

提前致谢。

回答如下:

我发现使用request.body.PARAM_NAME可以正常工作。

发布评论

评论列表 (0)

  1. 暂无评论