user4813855
user4813855

Reputation:

Retrofit in android?

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}

Upvotes: 1

Views: 1703

Answers (2)

A-Droid Tech
A-Droid Tech

Reputation: 2321

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

Upvotes: 0

Rajesh Gauswami
Rajesh Gauswami

Reputation: 572

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")

Upvotes: 2

Related Questions