POST原始数据使用Retrofit

POST原始数据使用Retrofit

问题描述:

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

我发现许多解决方案POST JSON在身体使用排球,但我发送的数据不是JSON。 我的数据是:{project_purpose:[执行]}

而从邮递员打,我得到的数据,但不是机器人。

请告诉我如何做到这一点。

我想发送的字符串,但在错误代码越来越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); 

试试这个

原始形式试图POST数据,当我面临着同样的问题只为此,我得到我的解决方案后,我浪费了整整一天。

  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", "[email protected]"); 
        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(); 
    } 
    

输出

logcat的: enter image description here

邮差 enter image description here

注意:-i我没有使用任何模型类后得到的数据,检索数据,您可以使用反正来存储数据。