简体   繁体   English

如何为 Android java 编写/转换 CURL

[英]How to write / convert CURL for Android java

I am trying to implement the MOT history API https://dvsa.github.io/mot-history-api-documentation/ and they give an example using CURL which works with the supplied api key successfully when using an online CURL tool. I am trying to implement the MOT history API https://dvsa.github.io/mot-history-api-documentation/ and they give an example using CURL which works with the supplied api key successfully when using an online CURL tool.

I am trying to implement this in Android and realise I have to use something like HttpPost rather than CURL, this is my code:我试图在 Android 中实现这一点,并意识到我必须使用类似 HttpPost 而不是 CURL,这是我的代码:

    //Tried with full URL and by adding the registration as a header.
    //HttpPost httpPost = new HttpPost("https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests?registration=" + reg_selected);
     HttpPost httpPost = new HttpPost("https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests");

     httpPost.addHeader("Content-Type", "application/json");
     httpPost.addHeader("Accept", "application/json+v6");
     httpPost.addHeader("x-api-key", "abcdefgh123456");
     httpPost.addHeader("registration", reg_selected);

     StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8);
     httpPost.setEntity(entity);
     HttpClient client = new DefaultHttpClient();

     try {
          HttpResponse response = client.execute(httpPost);

          if (response.getStatusLine().getStatusCode() == 200) {

               InputStream inputStream = response.getEntity().getContent();
               bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
               String readLine = bufferedReader.readLine();

               String jsonStr = readLine;
               JSONObject myJsonObj = new JSONObject(jsonStr);

               
          }else if (response.getStatusLine().getStatusCode() == 400){
                 //Bad Request  Invalid data in the request. Check your URL and parameters
                 error_text = "Bad Request";
          }else if (response.getStatusLine().getStatusCode() == 403){
                //Unauthorised – The x-api-key is missing or invalid in the header
                error_text = "Authentication error"; //<<<< FAILS HERE 403
          }

response.getStatusLine().getStatusCode() returns • "403 – Unauthorised – The x-api-key is missing or invalid in the header". response.getStatusLine().getStatusCode() 返回 • “403 – 未经授权 – x-api-key 在标头中丢失或无效”。 However the x-api-key that I use works correctly with the online CURL test so the actual key is correct but how I am adding it to my android code request must be invalid or similar.但是,我使用的 x-api-key 与在线 CURL 测试一起正常工作,因此实际密钥是正确的,但是我如何将其添加到我的 android 代码请求中的方式必须无效或类似。

Can anyone throw any light as to the correct way to convert the CURL into Android java so that the server does not return 403?任何人都可以了解将 CURL 转换为 Android java 以使服务器不返回 403 的正确方法吗?

Thanks谢谢

It's easy to do with Jsoup :使用Jsoup很容易做到:

    // CREATE CONNECTION
    Connection conn=Jsoup.connect("URL_GOES_HERE");
    
    // ADD POST/FORM DATA
    conn.data("KEY", "VALUE");
    
    // ADD HEADERS HERE
    conn.header("KEY", "VALUE");
    
    // SET METHOD AS POST 
    conn.method(Connection.Method.POST);
    
    // ACCEPT RESPONDING CONTENT TYPE
    conn.ignoreContentType(true);
    
    try
    {
        // GET RESPONSE
        String response = conn.execute().body();
        
        // USE RESPONSE HERE
        // CREATE JSON OBJECT OR ANYTHING...
    } catch(HttpStatusException e)
    {
        int status = e.getStatusCode();
        // HANDLE HTTP ERROR HERE
    } catch (IOException e)
    {
        // HANDLE IO ERRORS HERE
    }

Ps: I guess you are confused with Header and Post Data . Ps:我猜你对HeaderPost Data感到困惑。 The key etc ( Credentials ) must be used as Post Data and Content Type etc as Header .密钥等(凭据)必须用作Post Data和内容类型等,例如Header

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

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