1

I'm new to Retrofit. How can I send param and get Json from bellow url ?

http://xxx:8087/courier.svc/login?username=jim&password=123456

I need to a link for tutorial .

This code is in my MainActivity :

private void loadJSON(String username, String password) {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://192.168.8.11:8087/sample.svc/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    RequestInterface_Login request = retrofit.create(RequestInterface_Login.class);
    Call<JSONResponseLogin> call = request.getJSON(username, password);
    call.enqueue(new Callback<JSONResponseLogin>() {
        @Override
        public void onResponse(Call<JSONResponseLogin> call, Response<JSONResponseLogin> response) {

            JSONResponseLogin jsonResponse = response.body();
            data = new ArrayList<>(Arrays.asList(jsonResponse.getLogin()));
        }

        @Override
        public void onFailure(Call<JSONResponseLogin> call, Throwable t) {
            Log.d("Error", t.getMessage());
        }
    });
}

My ModelLogin :

public class ModelLogin {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }
}

My RequestInterface_Login :

public interface RequestInterface_Login {

    @GET("/login/{username}/{password}")
    Call<JSONResponseLogin> getJSON(@Path("username") String username, @Path("password") String password);
}

My JSONResponseLogin :

public class JSONResponseLogin {
    private ModelLogin[] login;

    public ModelLogin[] getLogin() {
        return login;
    }
}

But get me NullPointerException!

enter image description here

I get json from service same bellow :

{"Key":null,"Response":1}
  • Is the API Hit in the form of field or JSON Response or it is directly to be entered in URL? – Kshitij Jain Oct 05 '16 at 06:20
  • My library is : compile 'com.squareup.retrofit2:retrofit:2.1.0' compile ('com.squareup.retrofit2:converter-gson:2.0.0-beta4') { exclude module: 'retrofit' } –  Oct 05 '16 at 06:22
  • I am not talking about retrofit. If you could share actual API Link, I can help you with it – Kshitij Jain Oct 05 '16 at 06:24
  • 2
    Have a look at this documentation. [https://futurestud.io/tutorials/android-basic-authentication-with-retrofit](https://futurestud.io/tutorials/android-basic-authentication-with-retrofit) And there are many more basic examples also how to process http parameters – Vall0n Oct 05 '16 at 06:25
  • @kshitij jain . I edited my question . –  Oct 05 '16 at 06:33
  • Hey @JoJoRoid please add your error log. – Mayur Patel Oct 05 '16 at 06:39
  • Please Change your class with add (@)SerializedName("fieldName"). Example : http://www.pratikbutani.com/2016/05/android-tutorial-json-parsing-using-retrofit-part-1/ – Mayur Patel Oct 05 '16 at 07:10

2 Answers2

2

Before you get call the retrofit you can just print the URL and then you can load URL in browser and see what response is coming you can add log by bellow line before call.enqueue(new Callback<JSONResponseLogin>()

Log.e(TAG, "API URL: " + call.request().url());

Check your response And let me know I will help you ... coz i am using retrofit in my 3 projects

Do like this ...in interface

 @GET("/courier.svc/login?)
 Call<JSONResponseLogin> getJSON(@Query("username") String username,
                                 @Query("password") String password);

and remove it from base .baseUrl("http://192.168.8.11:8087")

RKRK
  • 1,284
  • 5
  • 14
  • 18
Rajesh Gauswami
  • 572
  • 7
  • 22
  • It show me :http://192.168.8.11:8087/login/jim/123456 but I need to http://192.168.8.11:8087/courier.svc/login?username=jim&password=123456 –  Oct 05 '16 at 06:54
  • i have edited answer see do this and let me know if its works @JoJoRoid – Rajesh Gauswami Oct 05 '16 at 07:02
  • I cane see true URL now .but get me error java.lang.NullPointerException: storage == null I think that it is for {"Key":null,"Response":1} ? –  Oct 05 '16 at 07:06
  • are you using any authentication like apikey or secrete key to call url? @JoJoRoid – Rajesh Gauswami Oct 05 '16 at 07:07
  • run api in https://www.getpostman.com/ first and see response make sure you don't required extra query para to call url @JoJoRoid – Rajesh Gauswami Oct 05 '16 at 07:10
  • When I logged get me a secrete key.But I don't need to secrete key now . –  Oct 05 '16 at 07:14
  • Okay so use API in postman and get your proper response from there and let me know what you are suppose to pass in parameter because now all things are correct in retrofit. is there any header you suppose to pass? – Rajesh Gauswami Oct 05 '16 at 07:17
  • My true url is : http://192.168.8.11:8087/courier.svc/login?username=asheq&password=123456 . I get this url . –  Oct 05 '16 at 07:19
  • Don't solve my problem get me java.lang.NullPointerException: storage == null. But my url is true . –  Oct 05 '16 at 07:32
0

I am writing this post on 'Retrofit in android' using our latest Retrofit library, This plugin can be be used to Integrate Retrofit to your project. Before using reftrofit, we just need to follow some steps to add QAssist Plugins in Android studio which will reduce your efforts of Webservices integration.

1.Add (QAssist - Android Studio Plugin) Android plugin in your Android studio. ( https://github.com/sakkeerhussain/QAssist ).:-

2.When we done with the plugin then we need to restart our Android studio.

3.Check your Android studio Menubar You can find "QAssist".

4.Click on that and Start with Retrofit Integrate.

5.Now we need : - BaseUrl of Web Api's - All Api's en points. - Sample request and Response of All request. - After completing these steps your reftrofit(QAssist/retrofit package is genrated inside Project).

6.You can find A Class and One Interface inside that Package.

7.The interface contain all the endpoints detail and Class will cotain the Retrofit connection details.

8.We just need to add a small code to access and Api and Parse that Api' response to Data model. 9.Use this sample function to call Api.

private void demoRetroFitCall() {
        APIService apiService = RetrofitManager.getInstance(this);
        //APIService apiService = RetroFitApiClient.getClient().create(APIService.class);
        Call<ModelData> call = apiService.TestGet();
        call.enqueue(new Callback<ModelData>() {
            @Override
            public void onResponse(Call<ModelData> call, Response<ModelData> response) {
                Log.d("TAG", "Data recived in response.body");            
                }

            @Override
            public void onFailure(Call<ModelData> call, Throwable t) {
                Log.e("TAG", t.toString());            
                }
        });
    }
56 - voda
Neeraj Kumar
  • 771
  • 2
  • 16
  • 37
A-Droid Tech
  • 2,301
  • 24
  • 33