android raw请求,android - How to POST raw whole JSON in the body of a Retrofit request? - Stack Overfl...

1)Add dependencies-

compile 'com.google.code.gson:gson:2.6.2'

compile 'com.squareup.retrofit2:retrofit:2.3.0'

compile 'com.squareup.retrofit2:converter-gson:2.3.0'

2) make Api Handler class

public class ApiHandler {

public static final String BASE_URL = "URL";

private static Webservices apiService;

public static Webservices getApiService() {

if (apiService == null) {

Gson gson = new GsonBuilder()

.setLenient()

.create();

Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create(gson)).baseUrl(BASE_URL).build();

apiService = retrofit.create(Webservices.class);

return apiService;

} else {

return apiService;

}

}

}

3)make bean classes from Json schema 2 pojo

Remember

-Target language : Java

-Source type : JSON

-Annotation style : Gson

-select Include getters and setters

-also you may select Allow additional properties

4)make interface fro api calling

public interface Webservices {

@POST("ApiUrlpath")

Call ApiName(@Body JsonObject jsonBody);

}

if you have a form-data parameters then add below line

@Headers("Content-Type: application/x-www-form-urlencoded")

Other way for form-data parameter check this link

5)make JsonObject for passing in to body as parameter

private JsonObject ApiJsonMap() {

JsonObject gsonObject = new JsonObject();

try {

JSONObject jsonObj_ = new JSONObject();

jsonObj_.put("key", "value");

jsonObj_.put("key", "value");

jsonObj_.put("key", "value");

JsonParser jsonParser = new JsonParser();

gsonObject = (JsonObject) jsonParser.parse(jsonObj_.toString());

//print parameter

Log.e("MY gson.JSON: ", "AS PARAMETER " + gsonObject);

} catch (JSONException e) {

e.printStackTrace();

}

return gsonObject;

}

6) Call Api Like this

private void ApiCallMethod() {

try {

if (CommonUtils.isConnectingToInternet(MyActivity.this)) {

final ProgressDialog dialog;

dialog = new ProgressDialog(MyActivity.this);

dialog.setMessage("Loading...");

dialog.setCanceledOnTouchOutside(false);

dialog.show();

Call registerCall = ApiHandler.getApiService().ApiName(ApiJsonMap());

registerCall.enqueue(new retrofit2.Callback() {

@Override

public void onResponse(Call registerCall, retrofit2.Response response) {

try {

//print respone

Log.e(" Full json gson => ", new Gson().toJson(response));

JSONObject jsonObj = new JSONObject(new Gson().toJson(response).toString());

Log.e(" responce => ", jsonObj.getJSONObject("body").toString());

if (response.isSuccessful()) {

dialog.dismiss();

int success = response.body().getSuccess();

if (success == 1) {

} else if (success == 0) {

}

} else {

dialog.dismiss();

}

} catch (Exception e) {

e.printStackTrace();

try {

Log.e("Tag", "error=" + e.toString());

dialog.dismiss();

} catch (Resources.NotFoundException e1) {

e1.printStackTrace();

}

}

}

@Override

public void onFailure(Call call, Throwable t) {

try {

Log.e("Tag", "error" + t.toString());

dialog.dismiss();

} catch (Resources.NotFoundException e) {

e.printStackTrace();

}

}

});

} else {

Log.e("Tag", "error= Alert no internet");

}

} catch (Resources.NotFoundException e) {

e.printStackTrace();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值