android - 为什么使用android改造时没有响应,虽然我可以看到请求?
问题描述
我正在使用 android 改造向我的 laravel 服务器发送一个发布请求。我面临的是我可以在服务器端获取请求正文,但是没有响应(onResponse 方法不运行)。API接口:
package com.example.asus.retrofit;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;
public interface Api {
String BASE_URL="http://405d6d4f.ngrok.io/";
@POST("execute")
@FormUrlEncoded
Call<ServerJsonObject> sendData(@Field("name") String name);
}
我的 ServerJsonObject 类:
public class ServerJsonObject {
private String name;
private int age;
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
主要活动:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Api api = retrofit.create(Api.class);
Call<ServerJsonObject> call = api.sendData("Mathew");
call.enqueue(new Callback<ServerJsonObject>() {
@Override
public void onResponse(Call<ServerJsonObject> call, Response<ServerJsonObject> response) {
ServerJsonObject x = response.body(); // it doesn't enter here when debugging
Log.d("data",Integer.toString(x.getAge()));
}
@Override
public void onFailure(Call<ServerJsonObject> call, Throwable t) {
}
});
}
}
我的服务器返回的响应:
{“姓名”:“马修”,“年龄”:22}
我可以在服务器端看到请求正文,如下所示:
名称=马修
解决方案
推荐阅读
- python - 我正在使用 Python 开发一个不和谐的机器人。每次我运行程序,错误都会显示“未找到不和谐模块”。我使用 pip 安装了 discord.py
- node.js - Alexa:响应来自服务的响应(节点 v2 SDK)
- java - 如何在可执行文件中包含带有 pyinstaller 的特定 .jar 文件?
- flask-oauthlib - Authlib - 隐式流
- c++ - 使用向量从c ++中的文件中反转字符串
- prolog - 不确定单例变量警告和未绑定变量错误
- python - Django Admin Inlines 和 AbstractUser
- unity3d - Google Poly 运行时在 iPhone 上占用 1GB RAM
- java - 无法执行目标 org.codehaus.mojo:exec-maven-plugin:1.2.1:exec 错误与 neo4j
- c# - 从一维数组查看 Reportviewer 中的数据表