POST原始数据使用Retrofit

时间:2017-09-21 19:05:42

标签: android web-services retrofit2

我正在尝试使用Retrofit发布原始数据。

我在Body中使用volley发现了许多POST JSON解决方案,但我发送的数据不是JSON。 我的数据是: {project_purpose:[EXECUTION]}

从邮递员那里来的时候,我得到了数据但不是在android中。

请建议我如何做到这一点。

我正在尝试以字符串形式发送,但在错误代码中获得500

我也在JsonObject中发送数据,但没有工作..

这是我要调用的代码..

 String bodyST = "{project_purpose: [purpose]}";

 OR

 JsonObject data = new JsonObject();
 JSONArray jarray = new JSONArray();
 jarray.put("EXECUTION");
 data.addProperty("project_purpose", String.valueOf(jarray));


 Call<JsonArray> call = apiInterface.getData(mAuthToken, "application/json", bodyST);

1 个答案:

答案 0 :(得分:0)

试试这个

在尝试 仅以原始格式发布数据 时,我遇到了同样的问题,为此,在我获得解决方案后,我浪费了一整天。< / p>

  1. 您的API界面应如下所示: -

    @POST(Constants.CONTACTS_URL)
    Call<Object> getUser(@Body Map<String, String> body);
    
  2. 在你打电话给你的班级

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constants.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    ApiInterface apiInterface = retrofit.create(ApiInterface.class);
    
    try {
        Map<String, String> requestBody = new HashMap<>();
        requestBody.put("email", "davinder.codeapex@gmail.com");
        requestBody.put("password", "12345678");
        Call<Object> call=apiInterface.getUser(requestBody);
        call.enqueue(new Callback<Object>() {
            @Override
            public void onResponse(Call<Object> call, Response<Object> response) {
                try {
                    JSONObject object=new JSONObject(new Gson().toJson(response.body()));
                    Log.e("TAG", "onResponse: "+object );
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<Object> call, Throwable t) {
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
    
  3. 输出

    logcat的: fully compliant, but not working with these tools

    邮差 enter image description here

    注意: - 我没有使用任何模型类来获取数据,检索您可以用来存储数据的数据。

相关问题