Java 响应 ;状态 ;代码::404改装2.0
据我所知,我之所以会遇到这个问题,是因为我的代码有错误,但我能找到确切的原因 这是我的密码Java 响应 ;状态 ;代码::404改装2.0,java,android,retrofit,retrofit2,Java,Android,Retrofit,Retrofit2,据我所知,我之所以会遇到这个问题,是因为我的代码有错误,但我能找到确切的原因 这是我的密码 public class MainActivity extends AppCompatActivity { private MyServerConnection connection; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); se
public class MainActivity extends AppCompatActivity {
private MyServerConnection connection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String URL = ...;
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl(URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
connection = retrofit.create(MyServerConnection.class);
}
interface MyServerConnection {
@POST("/appreg")
Call<JSONObject> postWithJson(@Body JSONObject object);
}
public void test(View view) {
Log.e("TAG", "---!!! TEST !!!---");
final Call<JSONObject> call = connection.postWithJson(getJson());
call.enqueue(new Callback<JSONObject>() {
@Override
public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {
Log.e("Response status code: ", String.valueOf(response.code()));
// isSuccess is true if response code => 200 and <= 300
if (response.isSuccessful()) {
Log.e("response body : ", response.body().toString());
} else {
try {
Log.e("response body error : ", response.errorBody().string());
here i get NULL ---> Log.e("!!!-- response body : ", " " + response.body());
} catch (IOException ignored) {
}
}
}
@Override
public void onFailure(Call<JSONObject> call, Throwable t) {
Log.e("TAG", "---!!! ERROR !!!--- : " + t.getMessage());
}
});
}
private JSONObject getJson() {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("key", "val");
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
这很奇怪,因为若我把一个URL放在浏览器中,我会得到标准响应
{ "data": null, "service": null, "status": { "code": 1, "description": "An error occurs while processing your request, Please try later or contact our sales department", "status": "success" } }
如何使用我的应用程序获得相同的标准消息
我做错了什么?最终我在我的案例中找到了原因,原因是
/
我把这首歌/
放在.baseUrl(URL)
的URL末尾,我还把它放在方法@POST(“/appreg”)
当我从类似于这样的
@POST(“appreg”)
的方法中删除它时,它开始工作了最终,我在我的案例中找到了原因,原因是/
我把这首歌/
放在.baseUrl(URL)
的URL末尾,我还把它放在方法@POST(“/appreg”)
当我从像这样的
@POST(“appreg”)
方法中删除它时,它开始工作在我的例子中,问题是@Path
我在做下面的事情
@Headers("Content-Type:application/json")
@GET("{remaningUrl}")
Call<String> postWithJson(@Header("Authorization") String authorization,@Path String remaningUrl);
@Headers(“内容类型:application/json”)
@获取(“{Remingurl}”)
调用postWithJson(@Header(“Authorization”)字符串Authorization,@Path String reminingUrl);
通过使用@Url解决
@Headers("Content-Type:application/json")
@GET
Call<String> postWithJson(@Header("Authorization") String authorization,@Url String remaningUrl);
@Headers(“内容类型:application/json”)
@得到
调用postWithJson(@Header(“Authorization”)字符串Authorization,@Url字符串reminingUrl);
在我的例子中,问题是@Path
我在做下面的事情
@Headers("Content-Type:application/json")
@GET("{remaningUrl}")
Call<String> postWithJson(@Header("Authorization") String authorization,@Path String remaningUrl);
@Headers(“内容类型:application/json”)
@获取(“{Remingurl}”)
调用postWithJson(@Header(“Authorization”)字符串Authorization,@Path String reminingUrl);
通过使用@Url解决
@Headers("Content-Type:application/json")
@GET
Call<String> postWithJson(@Header("Authorization") String authorization,@Url String remaningUrl);
@Headers(“内容类型:application/json”)
@得到
调用postWithJson(@Header(“Authorization”)字符串Authorization,@Url字符串reminingUrl);
在改装2中
当在基本URL的末尾和端点URL的开头给出/
时,将在响应中产生空响应404。因此,我坚持在基本URL使用/
,这对我有效,希望对您也有效。:) 在改装2中
当在基本URL的末尾和端点URL的开头给出
/
时,将在响应中产生空响应404。因此,我坚持在基本URL使用/
,这对我有效,希望对您也有效。:) URL或RestApi不存在……。一个原因可能是您无法直接发布json对象。构建hashmap而不是json。并在响应中使用gson库的JsonElement而不是Jsonwhat您从Log.e(“响应体错误:”,response.body()
而不是Log.e(“响应体错误:”,response.errorBody().string())代码>?将URL放入浏览器时,可能执行了GET命令。您的代码尝试了POST命令。@这是不可能的,因为我复制了此URL并将其放在浏览器中,然后检索respond{“data”:null,“service”:null,“status”:{“code”:1,“description”:“处理您的请求时出错,请稍后再试或与我们的销售部门联系”,“status”:“success”}
URL或RestApi不存在……。一个原因可能是您无法直接发布json对象。构建hashmap而不是json。并在响应中使用gson库的JsonElement而不是Jsonwhat您从Log.e(“响应体错误:”,response.body()
而不是Log.e(“响应体错误:”,response.errorBody().string())代码>?将URL放入浏览器时,可能执行了GET命令。您的代码尝试了POST命令。@这是不可能的,因为我复制了此URL并将其放在浏览器中,然后检索respond{“data”:null,“service”:null,“status”:{“code”:1,“description”:“处理您的请求时出错,请稍后再试或与我们的销售部门联系”,“status”:“success”}