Json 不会到达
Json wont arrive
我的朋友编写了 API,我必须在 Android 上编写前端代码。但是 json 不会到达。我知道我的代码有效,因为我在 3 个不同的测试仪上对其进行了测试,他的 api 也有效。他也测试了几次。但是,如果我向没有数据发送任何数据。请求到达但数据未到达。响应也有效,但它总是说字符串为空。我在自己的计算机上托管 api。可能是虚拟设备无法向其托管的电脑发送任何内容吗?我希望有人能帮助我。
这是我的代码:
主要活动
public void post(String pswd, String name){
TextView textViewResult = findViewById(R.id.responses);
Post post = new Post(name,pswd);
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://localhost:5000")
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
API api = retrofit.create(API.class);
Call<Post> call = api.createPost(post);
call.enqueue(new Callback<Post>() {
@Override
public void onResponse(Call<Post> call, Response<Post> response) {
if(!response.isSuccessful()){
textViewResult.setText("Code: " + response.code());
return;
}
Post postResponse = response.body();
String content = "";
content += "Code: " + response.code() + "\n";
content += "token: " + postResponse.getToken() + "\n";
content+= "msg: " +postResponse.getMsg();
Endcontent = content;
}
@Override
public void onFailure(Call<Post> call, Throwable t) {
Endcontent = t.getMessage();
}
});
}
Api:
import java.util.List;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
public interface API {
@GET("posts")
Call<List<Get>> getPosts();
@POST("api/gen_token")
Call<Post> createPost(@Body Post post);
}
和Post:
public class Post {
private String name;
private String pswd;
private String token;
private String msg;
public Post(String name, String pswd) {
this.name = name;
this.pswd = pswd;
}
public String getPswd() {
return pswd;
}
public String getMsg(){
return msg;
}
public String getName() {
return name;
}
public String getToken() {
return token;
}
}
检查您分配给它们的字符串名称和字符串密码值
我的朋友编写了 API,我必须在 Android 上编写前端代码。但是 json 不会到达。我知道我的代码有效,因为我在 3 个不同的测试仪上对其进行了测试,他的 api 也有效。他也测试了几次。但是,如果我向没有数据发送任何数据。请求到达但数据未到达。响应也有效,但它总是说字符串为空。我在自己的计算机上托管 api。可能是虚拟设备无法向其托管的电脑发送任何内容吗?我希望有人能帮助我。
这是我的代码:
主要活动
public void post(String pswd, String name){
TextView textViewResult = findViewById(R.id.responses);
Post post = new Post(name,pswd);
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://localhost:5000")
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
API api = retrofit.create(API.class);
Call<Post> call = api.createPost(post);
call.enqueue(new Callback<Post>() {
@Override
public void onResponse(Call<Post> call, Response<Post> response) {
if(!response.isSuccessful()){
textViewResult.setText("Code: " + response.code());
return;
}
Post postResponse = response.body();
String content = "";
content += "Code: " + response.code() + "\n";
content += "token: " + postResponse.getToken() + "\n";
content+= "msg: " +postResponse.getMsg();
Endcontent = content;
}
@Override
public void onFailure(Call<Post> call, Throwable t) {
Endcontent = t.getMessage();
}
});
}
Api:
import java.util.List;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
public interface API {
@GET("posts")
Call<List<Get>> getPosts();
@POST("api/gen_token")
Call<Post> createPost(@Body Post post);
}
和Post:
public class Post {
private String name;
private String pswd;
private String token;
private String msg;
public Post(String name, String pswd) {
this.name = name;
this.pswd = pswd;
}
public String getPswd() {
return pswd;
}
public String getMsg(){
return msg;
}
public String getName() {
return name;
}
public String getToken() {
return token;
}
}
检查您分配给它们的字符串名称和字符串密码值