Java 如何使用改造 2 发出 POST 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34776213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a POST request using retrofit 2?
提问by Cool Do
I was only able to run the hello world example (GithubService) from the docs.
我只能从文档中运行 hello world 示例 (GithubService)。
The problem is when I run my code, I get the following Error, inside of onFailure()
问题是当我运行我的代码时,我收到以下错误,里面 onFailure()
Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $
使用 JsonReader.setLenient(true) 在第 1 行第 1 列路径 $ 接受格式错误的 JSON
My API takes POST params value, so no need to encode them as JSON, but it does return the response in JSON.
我的 API 采用 POST params 值,因此无需将它们编码为 JSON,但它确实以 JSON 格式返回响应。
For the response I got ApiResponse class that I generated using tools.
对于响应,我得到了使用工具生成的 ApiResponse 类。
My interface:
我的界面:
public interface ApiService {
@POST("/")
Call<ApiResponse> request(@Body HashMap<String, String> parameters);
}
Here is how I use the service:
以下是我如何使用该服务:
HashMap<String, String> parameters = new HashMap<>();
parameters.put("api_key", "xxxxxxxxx");
parameters.put("app_id", "xxxxxxxxxxx");
Call<ApiResponse> call = client.request(parameters);
call.enqueue(new Callback<ApiResponse>() {
@Override
public void onResponse(Response<ApiResponse> response) {
Log.d(LOG_TAG, "message = " + response.message());
if(response.isSuccess()){
Log.d(LOG_TAG, "-----isSuccess----");
}else{
Log.d(LOG_TAG, "-----isFalse-----");
}
}
@Override
public void onFailure(Throwable t) {
Log.d(LOG_TAG, "----onFailure------");
Log.e(LOG_TAG, t.getMessage());
Log.d(LOG_TAG, "----onFailure------");
}
});
采纳答案by Vishal Raj
If you don't want JSON encoded params use this:
如果您不想使用 JSON 编码的参数,请使用以下命令:
@FormUrlEncoded
@POST("/")
Call<ApiResponse> request(@Field("api_key") String apiKey, @Field("app_id") String appId);
回答by Philipp Hellmayr
You should be aware of how you want to encode the post params. Important is also the @Header
annotation in the following. It is used to define the used content type in the HTTP header.
您应该知道如何对 post 参数进行编码。@Header
下面的注释也很重要。它用于定义 HTTP 标头中使用的内容类型。
@Headers("Content-type: application/json")
@POST("user/savetext")
public Call<Id> changeShortText(@Body MyObjectToSend text);
You have to encode your post params somehow. To use JSON for transmission you should add .addConverterFactory(GsonConverterFactory.create(gson))
into your Retrofit declaration.
您必须以某种方式对您的帖子参数进行编码。要使用 JSON 进行传输,您应该添加.addConverterFactory(GsonConverterFactory.create(gson))
到您的 Retrofit 声明中。
Retrofit restAdapter = new Retrofit.Builder()
.baseUrl(RestConstants.BASE_URL)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.client(httpClient)
.build();
Another source of your problem could be that the JSON, that's coming from the rest backend seems to be not correct. You should check the json syntax with a validator, e.g. http://jsonlint.com/.
您问题的另一个来源可能是来自其余后端的 JSON 似乎不正确。您应该使用验证器检查 json 语法,例如http://jsonlint.com/。