我如何发布一个原始的身体与翻新。如果我发现这个问题的许多版本,与解决方案一样,使用哈希Map,模型,JshonObject,字符串,或解析字符串到请求体。有人建议使用Gson的建设者。事情,我很难理解Retrofit。我对Retrofit和HTTP请求都是新手。我的代码感觉像是纸牌搭成的房子。登录后,我终于成功地在头中发送了一个JWT String,现在我想发出一个Post请求。
在《 Postman 》中,我用这个作为我的身体:
{"name":"PlanetX","radius":0}
我的一些尝试
//HashMap<String, Integer> body = new HashMap<String, Integer>();
//body.put("PlanetX", 0);
//Planet body = new Planet("PlanetX", 0);
//String text = "{\"name\":\"PlanetX\",\"radius\":0}";
//String text = "{\"name\":\"PlanetX\",\"radius\":0}";
//RequestBody body = RequestBody.create(MediaType.parse("text/plain"), text);
JsonObject body = new JsonObject();
body.addProperty("name", "value1");
body.addProperty("id", 9);
call = service.postObject(body);
这是我如何建立我的POST:
@POST("planets")
Call<String> postObject(@Body JsonObject body);
//Call<String> postObject(@Body RequestBody body);
//Call<String> postObject(@Body Planet body);
//Call<String> postObject(@Body HashMap<String, Integer> body);
大多数情况下,我会得到415状态或:
java.lang.IllegalArgumentException: Unable to create @Body converter for class com.google.gson.JsonObject (parameter #1)
for method PlanetRepository.postObject
public class Planet {
private long ID;
private String name;
private int radius;
public Planet(String name, int radius ) {
this.name = name;
this.radius = radius;
}
}
import android.content.Context;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;
public class RestService {
// Base URL
private static final String BASE_URL = "http://10.0.2.2:8080/";
// BUILDER GSON
private static Gson gson= new GsonBuilder()
.setLenient()
.create();
// BUILDER Retrofit
private static Retrofit.Builder builder
= new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(ScalarsConverterFactory.create());
//.addConverterFactory(GsonConverterFactory.create(gson));
private static Retrofit retrofit = builder.build();
// BUILDER OkHttpClient
private static OkHttpClient.Builder httpClient
= new OkHttpClient.Builder();
// BUILDER Logger
private static HttpLoggingInterceptor logging
= new HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BASIC);
// METHOD: Service Generator + Authentication
public static <S> S Create(Class<S> serviceClass, final String token ) {
if ( token != null ) {
httpClient.interceptors().clear();
httpClient.addInterceptor( chain -> {
Request original = chain.request();
Request request = original.newBuilder()
//.header("Authorization", token)
.header("Authorization", " Bearer "+token)
.build();
return chain.proceed(request);
});
httpClient.addInterceptor(logging); // Logger
builder.client(httpClient.build());
retrofit = builder.build();
}
return retrofit.create(serviceClass);
}
// METHOD: Message Generator
public static void Message(Context c, TextView v, Object o) {
String message;
if (o instanceof Response) {
message ="RESPONSE : \n"
+"HEADER :\n "
+ ((Response<?>) o).headers()
+"BODY: \n"
+ ((Response<?>) o).body()
+"\nSTATUS: \n"
+ ((Response<?>) o).code();
v.setText(message);
Toast.makeText(c, message, Toast.LENGTH_LONG).show();
} else if (o instanceof Throwable) {
message = "Caught Throwable: \n" + o;
v.setText(message);
Toast.makeText(c, message, Toast.LENGTH_LONG).show();
System.out.println(message);
} else if (o instanceof Exception) {
message = "Caught Exception: \n" + o;
v.setText(message);
Toast.makeText(c, message, Toast.LENGTH_LONG).show();
System.out.println(message);
} else {
System.out.println("ResponseMessage does not recognizes object");
}
}
}
1条答案
按热度按时间kq0g1dla1#
经过一番研究,我发现这个解决方案:
我的意见是
希望你不用这么麻烦。