使用翻新将JSON对象发布到服务器

时间:2018-09-03 07:08:15

标签: android retrofit

我尝试从我的android应用程序向服务器发送JSON对象,但是它似乎不起作用。没有错误存在,但它也不会将任何数据发送到服务器。以下是我的代码:

这是我的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());
            }
        });
    }
}

那我该怎么办,或者我做错了什么。任何帮助都感激不尽。预先谢谢你。

1 个答案:

答案 0 :(得分:0)

Retrofit的@Body注释会自动序列化数据。因此,您需要像下面那样传递身体。

创建POJO类

import com.google.gson.annotations.SerializedName;

public class Data {

   @SerializedName("name")
   private String name;
   @SerializedName("email")
   private String email;
   @SerializedName("phoneNumber")
   private String phoneNumber;

   public Data(String name, String email, String phoneNumber) {
      this.name = name;
      this.email = email;
      this.phoneNumber = phoneNumber;
   }

   // getter and setter methods
}

以上类的用法。

在API接口类中,修改方法参数。

Call<JsonObject> postRawJSON(@Body Data data); // from JSONObject to Data

onPostClicked(View view)方法内部

// code
Data d = new Data("test", "test@gmail.com", "9876543210");
Call<JsonObject> call = jsonPostService.postRawJSON(d);
call.enqueue(/* your code implementation here */);

现在尝试将请求(发布)发送到服务器。

PS :POJO类名称可以是任何名称。为了演示,我创建了Data