在安卓中改造? [英] Retrofit in android?
问题描述
我是 Retrofit 的新手.如何发送 param 并从 bellow url 获取 Json?
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
我需要一个教程链接.
此代码在我的 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());
}
});
}
我的ModelLogin
:
public class ModelLogin {
private String username;
private String password;
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
我的RequestInterface_Login
:
public interface RequestInterface_Login {
@GET("/login/{username}/{password}")
Call<JSONResponseLogin> getJSON(@Path("username") String username, @Path("password") String password);
}
我的JSONResponseLogin
:
public class JSONResponseLogin {
private ModelLogin[] login;
public ModelLogin[] getLogin() {
return login;
}
}
但是给我NullPointerException
!
我从下面的服务中得到 json
:
I get json
from service same bellow :
{"Key":null,"Response":1}
推荐答案
在调用改造之前,您只需打印 URL,然后您就可以在浏览器中加载 URL 并查看即将到来的响应您可以通过下面的行添加日志call.enqueue(new Callback
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());
检查您的回复让我知道我会帮助你......因为我在我的 3 个项目中使用改造
Check your response And let me know I will help you ... coz i am using retrofit in my 3 projects
像这样...在界面中
@GET("/courier.svc/login?)
Call<JSONResponseLogin> getJSON(@Query("username") String username,
@Query("password") String password);
并将其从基础 .baseUrl("http://192.168.8.11:8087")
这篇关于在安卓中改造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!