这是我得到的错误:
通用域名格式。谷歌。格森。JsonSyntaxException:java。lang.IllegalStateException:应为BEGIN_对象,但在第1行第1列路径处为字符串$
我从邮递员那里得到的JSON回复:
{
"title": "test title",
"body": "test body",
"userId": 1,
"id": 101
}
这就是我如何响应rest API(使用slim2框架)的响应:
$app->post('/posts', function() use($app){
$res = array(
"title" => $app->request->post('title'),
"body" => $app->request->post('body'),
"userId" => 1,
"id" => 101
);
echoResponse(201, $res);
});
回声响应方法:
function echoResponse($status_code, $response) {
$app = \Slim\Slim::getInstance();
$app->status($status_code);
$app->contentType('application/json');
echo json_encode($response);
}
调用API的方法:
public void sendPost(String title, String body) {
mAPIService.savePost(title, body, 1).enqueue(new Callback<Post>() {
@Override
public void onResponse(Call<Post> call, Response<Post> response) {
Log.i(TAG, "post sendPost to onResponse.");
if(response.isSuccessful()) {
showResponse(response.body().toString());
Log.i(TAG, "post submitted to API." + response.body().toString());
}
}
@Override
public void onFailure(Call<Post> call, Throwable t) {
Log.e(TAG, "Unable to submit post to API.");
Log.e(TAG, t.toString());
}
});
}
APIServie界面:
public interface APIService {
@POST("/posts")
@FormUrlEncoded
Call<Post> savePost(@Field("title") String title,
@Field("body") String body,
@Field("userId") long userId);
}
我如何获得改造
实例:
mAPIService = ApiUtils.getAPIService();
public class ApiUtils {
private ApiUtils() {}
public static final String BASE_URL = "http://myurl/v1/";
public static APIService getAPIService() {
return RetrofitClient.getClient(BASE_URL).create(APIService.class);
}
}
public class RetrofitClient {
private static Retrofit retrofit = null;
private static Gson gson = new GsonBuilder()
.setLenient()
.create();
public static Retrofit getClient(String baseUrl) {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
}
我发布所有这些的原因是我找不到问题出在哪里,因为我从API获得的JSON响应看起来不错,我调用API的方式看起来不错。有很多与此相关的问题可以在这里找到。但是我找不到问题的答案。请为我找到解决方案。谢谢
编辑:@Darpan和@Fred是对的。我启用了改装日志记录。上面是这么说的
D/OkHttp: --> POST http://myurl/posts http/1.1
D/OkHttp: Content-Type: application/x-www-form-urlencoded
D/OkHttp: Content-Length: 26
D/OkHttp: title=vz&body=gde&userId=1
D/OkHttp: --> END POST (26-byte body)
D/OkHttp: <-- 200 OK http://mywebsite/404/
<html of my 404 page>
D/OkHttp: <-- END HTTP (2119-byte body)
它给我服务器的404.html文件作为响应。但是当我从POSTMAN调用相同的API时,它会给我想要的结果。
所以基本上我可以从POSTMAN访问并获取结果,但不能使用改造从Android应用程序中使用它
知道怎么修吗?有什么问题吗?
我遇到了同样的错误,我的根本原因是我的Api期望调用<代码>https://my.url.comAndroid正在用http://my.url.com
。我们查看了日志,服务器将请求视为GET而不是POST。
将url更改为https://
后,效果很好。希望这对其他人有帮助
创建bellow类并更改调用
public class MyResponse {
@SerializedName("result")
private String result;
@SerializedName("data")
private Data data;
public class Data {
@SerializedName("title")
@Expose
private String title;
@SerializedName("body")
@Expose
private String body;
@SerializedName("userId")
@Expose
private String userId;
@SerializedName("id")
@Expose
private String id;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
}
您应该像这样更改json格式
{
"result" : "success",
"data" : {
"title":"title",
"body":"body",
"user_id":"userid",
"id":"id"
}
}
现在您可以访问它的响应,如下所示MyACK res=response.body();
还可以像这样打印响应日志。d(“响应”,new Gson()。toJson(res));
删除最后一个/
从您的基本url超文本传输协议://myurl/v1/
这就是它抛出404的原因。
问题内容: 我正在尝试向Swift发出发布请求。我的目标是将accesstoken facebook发布到服务器,但不起作用。这是代码: 答案如下: 问题答案: 通过这种方式,您可以使用POST Web服务:
在我的spring MVC应用程序中,我试图发出一个简单的Post请求,但由于请求的主体,它不起作用。我在params中得到了嵌套对象,Spring抛出了这个异常: 下面是我的对象DTO: 和: 我拿到了这些日志:
问题内容: 跨域AJAX POST请求在Web浏览器(包括手机上的浏览器)上可以很好地工作,但不适用于使用以下工具构建的本机应用程序 我创建了一个登录表单,用户必须输入他们的登录凭据,然后由heroku上托管的服务器对其进行验证,如果输入了有效的凭据,则返回json 。 我的Ajax脚本: 解决此问题所采取的步骤: 域白名单 -config.xml 告诉jQuery允许跨域 要么 禁用缓存 任何帮
我正在尝试使用Chrome扩展Postman测试一个简单的PHP页面。当我发送URL参数时,脚本运行良好(例如变量在参数中可用)。当我将它们作为参数发送时,参数仅包含。 剧本: 我错过了什么?
我有一个两个Spring Boot应用程序。一个是进行rest调用的rest客户端。另一个只有Restendpoint的应用程序。 当rest客户机到达Restendpoint时,它会失败。 这是用于命中restendpoint的代码: 这是客户端尝试访问的其余endpoint: 这是我在带有restendpoint的应用程序中看到的错误: 为什么 Rest 调用适用于邮递员而不是我的 rest
我试图使用Axios,因为它是NodeJS中唯一具有异步/等待功能的模块。 我已经在Python脚本中有一个POST请求,它工作得很好,我正在尝试适应NodeJS服务器。 但我阻止了我的第一次发帖请求。如果我直接与请求模块一起使用,它工作得很好,请参阅我的代码: 使用Axios(或Axios实例,但稍后使用…)因为我需要保持我的会话(就像在Python中一样,它像一个符咒一样工作)。 而作为响应的