我需要对发布请求进行翻新,但是我有一个我不太了解的问题。在尝试代码之前,我使用 Postman 测试了api调用,并请求如下所示:
这是我的android代码:
public class API {
private static <T> T builder(Class<T> endpoint) {
return new Retrofit.Builder()
.baseUrl(Utils.API_BASE_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(endpoint);
}
public static AllRequests request() {
return builder(AllRequests.class);
}
}
编辑请求:
@POST("api/android-feedback")
@Headers({"Content-Type: application/x-www-form-urlencoded", "Authorization: F#@3FA@#Rad!@%!2s"})
Call<String> sendFeedback(@Body FeedbackBody body);
FeedbackBody:
public class FeedbackBody{
private final String email;
private final String feedback;
public FeedbackBody(String email, String feedback){
this.email = email;
this.feedback = feedback;
}
}
最后我构造了请求并等待响应,问题是我在onFail方法中收到消息
private void sendFeedbackRequest(){
API.request().sendFeedback(new FeedbackBody("testmeil@meil.com", "test feedback").enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
goToMainActivity();
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Toast.makeText(SplashScreenActivity.this, R.string.try_again_later, Toast.LENGTH_SHORT).show();
}
});
编辑: 仍然无法正常工作..我认为我想出了问题所在,因为服务器端等待简单的POST请求而没有Json格式,我认为Retrofit默认使用JSON格式,如果我发送POST请求并使用JSON格式化Body参数服务器将无法解析我的请求,是否有可能像在POSTMAN一样发送简单的POST请求而无需使用JSON格式化?
$ _ POST ['feedback'] ='blabla'; $ _POST ['email'] ='blabla ..';
如果他收到Json格式的请求就无法解析它,因此我收到失败响应。
答案 0 :(得分:0)
您正在使用Gson转换器工厂。创建代表您身体的单个对象,而不是使用所有单个参数,可能会更容易。这样,您应该能够简单地遵循Retrofit网站上的示例。enter link description here 还有许多站点可以让您为自己生成普通的旧Java对象,例如this one:
例如您的Api通话:
#include "functs.h"
MODULE_LICENSE("GPL");
#define DRIVER_AUTHOR "projecter"
#define DRIVER_DESC "Some echo driver"
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
int Major;
static int Device_Open = 0;
static char msg[BUF_LEN];
static char *msg_Ptr;
static struct file_operations fops = {
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release
};
//init _module functions
int init_module(void){
Major = register_chrdev(0, DEVICE_NAME, &fops);
if (Major < 0){
printk(KERN_ALERT "I have failed to load!\n");
return Major;
}
printk (KERN_ALERT "I was assigned major number %d\n" , Major);
printk (KERN_ALERT "Please create device with name \n mknod /dev/%s c %d 0 \n" ,DEVICE_NAME , Major );
return 0;
}
int device_open(struct inode * inode, struct file *file){
static int counter = 0;
if (Device_Open){
return -EBUSY;
}
Device_Open++;
sprintf(msg, "Good morning Dave, I was opened %d times", counter++);
msg_Ptr = msg;
try_module_get(THIS_MODULE);
return 0;
}
int device_release(struct inode * inode, struct file *file){
Device_Open--;
module_put(THIS_MODULE);
return 0;
}
ssize_t device_read(struct file * file, char * buffer, size_t length, loff_t *offset){
int bytes_read = 0;
if(*msg_Ptr == 0){
return 0;
}
while(length && *msg_Ptr){
put_user(* (msg_Ptr++), buffer++ );
length--;
bytes_read++;
}
return bytes_read;
}
char temp_str[20] = {};
ssize_t device_write(struct file * file , const char * buffer, size_t length, loff_t *offset){
static int MAX = 20;
if (buffer){
if(copy_from_user(temp_str, buffer , MAX) !=0 ){
return -EFAULT;
}
else{
printk(KERN_ALERT "DATA: %s \n\n", temp_str);
}
}
printk(KERN_ALERT "I am Sorry Dave, But I cannot Let you do that !\n");
memset(temp_str, 0, 20);
return 1 ;
// return -EINVAL;
}
还有您的FeedbackBody类:
@POST("api/android-feedback")
Call<String> sendFeedback(@Body FeedbackBody feedback);
答案 1 :(得分:0)
首先,您需要创建请求(POJO类)
public class FeedbackRequest {
public String email;
public String feedback;
}
当您致电sendFeedbackRequest()
时,像下面这样传递FeedbackRequest
”
FeedbackRequest req = new FeedbackRequest();
req.email= "email";
req.feedback= "feedback"
sendFeedbackRequest(req)
之后,您的sendFeedbackRequest()
应该像这样
private void sendFeedbackRequest(FeedbackRequest request){
API.request().sendFeedback(request).enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
goToMainActivity();
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Toast.makeText(SplashScreenActivity.this, R.string.try_again_later, Toast.LENGTH_SHORT).show();
}
});
您的改装要求应该是这样的
@FormUrlEncoded
@POST("api/android-feedback")
@Headers({"Content-Type: application/json", "Authorization: F31daaw313415"})
Call<String> sendFeedback(@Body FeedbackRequest request);
现在它应该可以工作了。随便问什么。
答案 2 :(得分:0)
Java:
@POST("/api/android-feedback")
Call<String> sendFeedback(@Body FeedbackBody feedback);
科特琳:
@POST("/api/android-feedback")
fun sendFeedback(@Body feedback: FeedbackBody): Call<String>
此外,可能您忘记了端点中的斜杠。