'POST raw data Using Retrofit
I'm trying to POST raw data using Retrofit.
I found many solution to POST JSON in Body using volley but the data I'm sending is not JSON. my data is : {project_purpose: [EXECUTION]}
while hitting from the postman, I'm getting the data but not in android.
Please suggest me how to do this.
I'm trying to send as string but getting 500 in error code
I've also send the data in JsonObject, but not working..
Here is my code to call..
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);
Solution 1:[1]
try this
I was facing the same problem when trying to POST data in raw form only and for this, i have wasted my whole day after that I got my solutions.
Your API interface should be like this:-
@POST(Constants.CONTACTS_URL) Call<Object> getUser(@Body Map<String, String> body);In your class where you are calling this
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(); }
Output
Note:-i am not using any model class to get data after, retrieving data you can use anyway to store data.
Solution 2:[2]
Just send your body as string
@PUT("your-endpoint")
fun yourRequsetFunction(@Body body : String) :Response<YourResponseType>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | mmdreza baqalpour |


