Loopj Put and Post with basic auth return null response with no error












0














This is an attempt to use Loopj for a Synchronous put and post call from a HTTP utility class. The code uses a syncrhonous client as it’s used inside an AsyncTask and some UI interactions depend heavily on the json response so the AsyncTask is managing making the call asynchronously.



All the get calls from the HTTP utility class are working successfully. The post and put do not and they both appear to have exactly the same problem.


The json string is created using Gson. I’ve tested the json output from the application directly in Postman and it posts exactly as expected to the API, so it appears to be well formed and behaves totally as expected without any errors.



Both the put and post calls are constructed without throwing an error. Basic authorization is being added (as shown on the client instance). The SyncHTTPClient put method is called using a null context parameter. I did a bit of research and found a single post where this was being done successfully.


https://github.com/loopj/android-async-http/issues/1139



The put call fires but doesn’t enter either the overridden methods of the handler. It just returns null. I've included a portion of the working class to view:



public void executePutSave(String name, String pass, String jsonBody) {
client.setBasicAuth(name, pass);
executeLoopJPutCall("/api/Save", jsonBody);
}

public void executeLoopJPutCall(String relativeUrl, String jsonBody) {
String url = getAbsoluteUrl(relativeUrl);
StringEntity entity = new StringEntity(jsonBody, "UTF-8");
jsonResponse = null;
client.put(null, url, entity, "application/json", new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
jsonResponse = response.toString();
Log.i(TAG, "onSuccess: " + jsonResponse);
}
@Override
public void onFailure(int statusCode, Header headers, Throwable throwable, JSONObject errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
jsonResponse = errorResponse.toString();
Log.e(TAG, "onFailure: " + statusCode + errorResponse );
}
}
);
}









share|improve this question





























    0














    This is an attempt to use Loopj for a Synchronous put and post call from a HTTP utility class. The code uses a syncrhonous client as it’s used inside an AsyncTask and some UI interactions depend heavily on the json response so the AsyncTask is managing making the call asynchronously.



    All the get calls from the HTTP utility class are working successfully. The post and put do not and they both appear to have exactly the same problem.


    The json string is created using Gson. I’ve tested the json output from the application directly in Postman and it posts exactly as expected to the API, so it appears to be well formed and behaves totally as expected without any errors.



    Both the put and post calls are constructed without throwing an error. Basic authorization is being added (as shown on the client instance). The SyncHTTPClient put method is called using a null context parameter. I did a bit of research and found a single post where this was being done successfully.


    https://github.com/loopj/android-async-http/issues/1139



    The put call fires but doesn’t enter either the overridden methods of the handler. It just returns null. I've included a portion of the working class to view:



    public void executePutSave(String name, String pass, String jsonBody) {
    client.setBasicAuth(name, pass);
    executeLoopJPutCall("/api/Save", jsonBody);
    }

    public void executeLoopJPutCall(String relativeUrl, String jsonBody) {
    String url = getAbsoluteUrl(relativeUrl);
    StringEntity entity = new StringEntity(jsonBody, "UTF-8");
    jsonResponse = null;
    client.put(null, url, entity, "application/json", new JsonHttpResponseHandler() {
    @Override
    public void onSuccess(int statusCode, Header headers, JSONObject response) {
    super.onSuccess(statusCode, headers, response);
    jsonResponse = response.toString();
    Log.i(TAG, "onSuccess: " + jsonResponse);
    }
    @Override
    public void onFailure(int statusCode, Header headers, Throwable throwable, JSONObject errorResponse) {
    super.onFailure(statusCode, headers, throwable, errorResponse);
    jsonResponse = errorResponse.toString();
    Log.e(TAG, "onFailure: " + statusCode + errorResponse );
    }
    }
    );
    }









    share|improve this question



























      0












      0








      0







      This is an attempt to use Loopj for a Synchronous put and post call from a HTTP utility class. The code uses a syncrhonous client as it’s used inside an AsyncTask and some UI interactions depend heavily on the json response so the AsyncTask is managing making the call asynchronously.



      All the get calls from the HTTP utility class are working successfully. The post and put do not and they both appear to have exactly the same problem.


      The json string is created using Gson. I’ve tested the json output from the application directly in Postman and it posts exactly as expected to the API, so it appears to be well formed and behaves totally as expected without any errors.



      Both the put and post calls are constructed without throwing an error. Basic authorization is being added (as shown on the client instance). The SyncHTTPClient put method is called using a null context parameter. I did a bit of research and found a single post where this was being done successfully.


      https://github.com/loopj/android-async-http/issues/1139



      The put call fires but doesn’t enter either the overridden methods of the handler. It just returns null. I've included a portion of the working class to view:



      public void executePutSave(String name, String pass, String jsonBody) {
      client.setBasicAuth(name, pass);
      executeLoopJPutCall("/api/Save", jsonBody);
      }

      public void executeLoopJPutCall(String relativeUrl, String jsonBody) {
      String url = getAbsoluteUrl(relativeUrl);
      StringEntity entity = new StringEntity(jsonBody, "UTF-8");
      jsonResponse = null;
      client.put(null, url, entity, "application/json", new JsonHttpResponseHandler() {
      @Override
      public void onSuccess(int statusCode, Header headers, JSONObject response) {
      super.onSuccess(statusCode, headers, response);
      jsonResponse = response.toString();
      Log.i(TAG, "onSuccess: " + jsonResponse);
      }
      @Override
      public void onFailure(int statusCode, Header headers, Throwable throwable, JSONObject errorResponse) {
      super.onFailure(statusCode, headers, throwable, errorResponse);
      jsonResponse = errorResponse.toString();
      Log.e(TAG, "onFailure: " + statusCode + errorResponse );
      }
      }
      );
      }









      share|improve this question















      This is an attempt to use Loopj for a Synchronous put and post call from a HTTP utility class. The code uses a syncrhonous client as it’s used inside an AsyncTask and some UI interactions depend heavily on the json response so the AsyncTask is managing making the call asynchronously.



      All the get calls from the HTTP utility class are working successfully. The post and put do not and they both appear to have exactly the same problem.


      The json string is created using Gson. I’ve tested the json output from the application directly in Postman and it posts exactly as expected to the API, so it appears to be well formed and behaves totally as expected without any errors.



      Both the put and post calls are constructed without throwing an error. Basic authorization is being added (as shown on the client instance). The SyncHTTPClient put method is called using a null context parameter. I did a bit of research and found a single post where this was being done successfully.


      https://github.com/loopj/android-async-http/issues/1139



      The put call fires but doesn’t enter either the overridden methods of the handler. It just returns null. I've included a portion of the working class to view:



      public void executePutSave(String name, String pass, String jsonBody) {
      client.setBasicAuth(name, pass);
      executeLoopJPutCall("/api/Save", jsonBody);
      }

      public void executeLoopJPutCall(String relativeUrl, String jsonBody) {
      String url = getAbsoluteUrl(relativeUrl);
      StringEntity entity = new StringEntity(jsonBody, "UTF-8");
      jsonResponse = null;
      client.put(null, url, entity, "application/json", new JsonHttpResponseHandler() {
      @Override
      public void onSuccess(int statusCode, Header headers, JSONObject response) {
      super.onSuccess(statusCode, headers, response);
      jsonResponse = response.toString();
      Log.i(TAG, "onSuccess: " + jsonResponse);
      }
      @Override
      public void onFailure(int statusCode, Header headers, Throwable throwable, JSONObject errorResponse) {
      super.onFailure(statusCode, headers, throwable, errorResponse);
      jsonResponse = errorResponse.toString();
      Log.e(TAG, "onFailure: " + statusCode + errorResponse );
      }
      }
      );
      }






      json post android-asynctask put loopj






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 14:15

























      asked Nov 10 at 0:12









      Wheres_the_money_Lebowski

      12




      12
























          1 Answer
          1






          active

          oldest

          votes


















          0














          So, apparently the header must be added explicitly when using the above code to Post or Put json to the API. Once I changed the header authentication line from this:



          client.setBasicAuth(name, pass);


          To this:



              String userpass = name + ":" + pass;
          String encoded = new String(Base64.encode(userpass.getBytes(),Base64.NO_WRAP));
          client.addHeader("Authorization", "Basic "+encoded);


          ...everything worked as expected.



          I found the information on this blog: https://github.com/loopj/android-async-http/issues/113



          Passing a null context worked, too.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234872%2floopj-put-and-post-with-basic-auth-return-null-response-with-no-error%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            So, apparently the header must be added explicitly when using the above code to Post or Put json to the API. Once I changed the header authentication line from this:



            client.setBasicAuth(name, pass);


            To this:



                String userpass = name + ":" + pass;
            String encoded = new String(Base64.encode(userpass.getBytes(),Base64.NO_WRAP));
            client.addHeader("Authorization", "Basic "+encoded);


            ...everything worked as expected.



            I found the information on this blog: https://github.com/loopj/android-async-http/issues/113



            Passing a null context worked, too.






            share|improve this answer


























              0














              So, apparently the header must be added explicitly when using the above code to Post or Put json to the API. Once I changed the header authentication line from this:



              client.setBasicAuth(name, pass);


              To this:



                  String userpass = name + ":" + pass;
              String encoded = new String(Base64.encode(userpass.getBytes(),Base64.NO_WRAP));
              client.addHeader("Authorization", "Basic "+encoded);


              ...everything worked as expected.



              I found the information on this blog: https://github.com/loopj/android-async-http/issues/113



              Passing a null context worked, too.






              share|improve this answer
























                0












                0








                0






                So, apparently the header must be added explicitly when using the above code to Post or Put json to the API. Once I changed the header authentication line from this:



                client.setBasicAuth(name, pass);


                To this:



                    String userpass = name + ":" + pass;
                String encoded = new String(Base64.encode(userpass.getBytes(),Base64.NO_WRAP));
                client.addHeader("Authorization", "Basic "+encoded);


                ...everything worked as expected.



                I found the information on this blog: https://github.com/loopj/android-async-http/issues/113



                Passing a null context worked, too.






                share|improve this answer












                So, apparently the header must be added explicitly when using the above code to Post or Put json to the API. Once I changed the header authentication line from this:



                client.setBasicAuth(name, pass);


                To this:



                    String userpass = name + ":" + pass;
                String encoded = new String(Base64.encode(userpass.getBytes(),Base64.NO_WRAP));
                client.addHeader("Authorization", "Basic "+encoded);


                ...everything worked as expected.



                I found the information on this blog: https://github.com/loopj/android-async-http/issues/113



                Passing a null context worked, too.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 at 18:19









                Wheres_the_money_Lebowski

                12




                12






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234872%2floopj-put-and-post-with-basic-auth-return-null-response-with-no-error%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Bressuire

                    Vorschmack

                    Quarantine