简体   繁体   English

Rest API 在 Postman 中工作,但在 Android 应用程序中无效

[英]Rest API's working in Postman but not in Android application

Rest API's are working in Postman with chrome, but same requests when tried to make from the android application works only in the following scenario: In postman execute a request(say a POST request A), run the android application I'm getting the expected result for POST REQUEST A. For a different POST request(say POST REQUEST B), again i have to execute the POST REQUEST B in POSTMAN and then do the action in my application. Rest API 在 Postman 和 chrome 中工作,但尝试从 android 应用程序发出的相同请求仅在以下情况下有效:在 postman 中执行请求(例如 POST 请求 A),运行 android 应用程序,我得到了预期的结果POST REQUEST A 的结果。对于不同的 POST 请求(比如 POST REQUEST B),我再次必须在 POSTMAN 中执行 POST REQUEST B,然后在我的应用程序中执行操作。 On not executing POST REQUEST B the response of POST REQUEST A is obtained in my application.在不执行 POST REQUEST B 时,在我的应用程序中获得了 POST REQUEST A 的响应。

Also after a certain period of timeout, if i simply run the android application that make POST requests, i get the following error: {"message":"Missing Authentication Token"}同样在一段时间超时后,如果我只是运行发出 POST 请求的 android 应用程序,我会收到以下错误:{"message":"Missing Authentication Token"}

The above error message is even obtained when the link provided in the url object is tried to access from a browser URL url = new URL("LINK");甚至当尝试从浏览器 URL url = new URL("LINK"); 访问 url 对象中提供的链接时,也会得到上述错误信息。

What could be the reasons for the above issues?出现上述问题的原因可能是什么?

Code snippet:

    URL url = new URL(LINK); 
    con = (HttpURLConnection) url.openConnection();
    InputStream errorStream = con.getErrorStream();
    if (errorStream != null) {
         BufferedReader br1 = new BufferedReader(new 
         InputStreamReader(errorStream));
         String errorInConnection = readStream(br1);
         Log.d(TAG, "errorInConnection:"+errorInConnection);
         }
    con.setRequestProperty("Authorization", "no auth");
    con.setRequestMethod("POST");
    con.setRequestProperty("Content-Type", "application/json");
    con.setRequestProperty("Accept", "application/json");
    con.setDoOutput(true);
    con.setChunkedStreamingMode(0);

In POSTMAN:

        {
             "data": {
                "currLat":some cordinate,
                "currLong":some coordinate
              },
              "datatype": "geodata"
        }

Header:
Key - Content-Type
Value - application/json

Corrections for my below answer are most welcome, also I would like to mention that I am new to android as well as handling REST API's非常欢迎对我的以下答案进行更正,另外我想提一下,我是 android 新手以及处理 REST API

The reason for why I was not getting a response is because I was not posting the data correctly.我没有得到回复的原因是我没有正确发布数据。 As mentioned, in my case I was suppose to post JSON data.如前所述,就我而言,我想发布 JSON 数据。 While posting JSON data even small mistakes like posting "latitude":12.321 for "latitude":"12.321" must not be done.在发布 JSON 数据时,即使是小错误,例如将"latitude":12.321发布为"latitude":"12.321"也不能做 The syntax must be followed.必须遵循语法。 If you are posting wrong data you will be getting the below type of error:如果您发布了错误的数据,您将收到以下类型的错误:

{"errorMessage":"java.lang.NullPointerException","errorType":"java.lang.NullPointerException","stackTrace":["java.io.Writer.write(Writer.java:157)","com.bosch.in.dataloggerlambda.StramDataProcessosLambda.handleRequest(StramDataProcessosLambda.java:102)","sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)","sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)","sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)","java.lang.reflect.Method.invoke(Method.java:498)"]}

If responses are obtained in postman then we will get the responses from HttpUrlConnection made from android studio.如果在 postman 中获得响应,那么我们将从 android studio 的 HttpUrlConnection 获得响应。

Since the url I had was for post and for post, data must be present to get a response, when I pasted the link in the browser it gave me the below error:由于我拥有的 url 用于发布和发布,因此必须存在数据才能获得响应,当我在浏览器中粘贴链接时,它给了我以下错误:

error: {"message":"Missing Authentication Token"}

The same thing was observed from android studio as well.从 android studio 也观察到了同样的事情。 There can be many other cases for which you might get the same error, I'm not sure of those.可能还有许多其他情况您可能会遇到相同的错误,我不确定这些。 But make sure even from android studio while trying to make a POST request you need to send the data.但是请确保即使是在 android studio 中尝试发出 POST 请求时,您也需要发送数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM