Java API返回404进行改装
我有一个函数,它返回一个简单的JSON响应(响应代码200)。但当我从android应用程序请求相同的URL时,它会得到404 这里是依赖项Java API返回404进行改装,java,android,nullpointerexception,retrofit,Java,Android,Nullpointerexception,Retrofit,我有一个函数,它返回一个简单的JSON响应(响应代码200)。但当我从android应用程序请求相同的URL时,它会得到404 这里是依赖项 compile 'com.squareup.retrofit:retrofit:2.0.0-beta2' compile 'com.squareup.okhttp:okhttp:2.6.0' compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2' compile 'com.google.code
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.okhttp:okhttp:2.6.0'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.google.code.gson:gson:2.4'
这是我的界面
public interface ExampleApi {
@GET("/test/drink/")
Call<Example> getExample();
}
注意:几年后,我对Android开发和使用Java完全陌生。请求您的服务
new Thread(new Runnable() {
@Override
public void run() {
Response response = App.retrofit().buildRequest(TestActivity.this).universityService().getExample(); //getExample("json")
if(response.getStatus() == 200){
System.out.println(response.getBody().toString());
}else{
System.out.println(response.getStatus());
}
}
}).start();
@GET("/test/drink/")
Response getExample();
为了得到你的服务
new Thread(new Runnable() {
@Override
public void run() {
Response response = App.retrofit().buildRequest(TestActivity.this).universityService().getExample(); //getExample("json")
if(response.getStatus() == 200){
System.out.println(response.getBody().toString());
}else{
System.out.println(response.getStatus());
}
}
}).start();
@GET("/test/drink/")
Response getExample();
或
你的模型
public class Example {
public boolean success;
public List<Drink> drinks;
}
public class Drink {
public String name;
public String description;
}
这对我很有用。这是一个语法问题
您必须这样编写您的服务:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://localpro.herokuapp.com/api/")
.addConverterFactory(GsonConverterFactory.create())
.build();
public interface ExampleApi {
@GET("test/drink/")
Call<Example> getExample();
reformation-reformation=new-reformation.Builder()
.baseUrl(“https://localpro.herokuapp.com/api/")
.addConverterFactory(GsonConverterFactory.create())
.build();
公共接口示例API{
@获取(“测试/饮用/”)
调用getExample();
有关更多详细信息,请查看Jake Wharton的精彩演示文稿:)检查您的android设备是否位于本地主机服务器的同一网络中,问题是您的应用程序找不到此url,并且将得到404未找到。API不在本地主机上。它在Internet上。@diogojmeThe url正在工作,路径
/test/drink/
看起来不错,问题似乎是您在response.body().toString()
中得到了一个“java.lang.NullPointerException”
,这是由NullBody引起的,我在我的应用程序示例中进行了测试,结果正常,404 not found与无效URL或路径有关,请确保您调用了正确的URL。我知道“NullPointerException”是由空正文引起的。我只需要知道URL如何无效。我在问题中提供了baseURL和终结点。您能给我发送一个指向您的工作示例应用程序代码的链接吗?@diogojmeYes,我将在几分钟后发布。我想知道改装的答案:2.0.0-beta2版本。
private static final String url = "http://localpro.herokuapp.com/api";
restAdapter = new RestAdapter.Builder()
.setEndpoint(url)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://localpro.herokuapp.com/api/")
.addConverterFactory(GsonConverterFactory.create())
.build();
public interface ExampleApi {
@GET("test/drink/")
Call<Example> getExample();