Я пытался отправить объект JSON на сервер из моего приложения для Android, но, похоже, он не работает. Нет ошибки, но она также не отправляет данные на сервер. Ниже приведен мой код:
Вот мой код ServiceGenerator.java
public class ServiceGenerator {
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
public static <S> S createService(Class<S> serviceClass, String baseUrl)
{
Retrofit builder = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
return builder.create(serviceClass);
}
}
Вот мой класс интерфейса
public interface IRetrofit {
@Headers({
"Accept: application/json",
"Content-Type: application/json"
})
@POST("saveRawJSONData")
Call<JsonObject> postRawJSON(@Body JsonObject jsonObject);
}
и мой MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onPostClicked(View view){
JsonObject jsonObject = new JsonObject();
JsonArray clientsArray = new JsonArray();
JsonObject clientsObject = new JsonObject();
clientsObject.addProperty("name", "test");
clientsObject.addProperty("email", "test@gmail.com");
clientsObject.addProperty("phoneNumber", "test");
clientsArray.add(clientsObject);
jsonObject.add("clients", clientsArray);
// Using the Retrofit
IRetrofit jsonPostService = ServiceGenerator.createService(IRetrofit.class, "http://192.168.137.1/originorders/clients/index/");
Call<JsonObject> call = jsonPostService.postRawJSON(jsonObject);
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
try{
Log.e("response-success", response.body().toString());
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
Log.e("response-failure", call.toString());
}
});
}
}
Так что мне делать, или я сделал что-то не так? Любая помощь будет высоко ценится. Заранее спасибо.