Java 如何在使用截取调用Util类中的web服务后返回值?
我想在截取web服务的响应后返回字符串值,在活动中返回调用值。 下面是我的代码 Utils.javaJava 如何在使用截取调用Util类中的web服务后返回值?,java,android,Java,Android,我想在截取web服务的响应后返回字符串值,在活动中返回调用值。 下面是我的代码 Utils.java public static String getitemCountPrice(String cartId) { try { if (Utils.isNetworkAvailable(mContext)) { HashMap<String, String> params = new HashMap<>();
public static String getitemCountPrice(String cartId) {
try {
if (Utils.isNetworkAvailable(mContext)) {
HashMap<String, String> params = new HashMap<>();
params.put(CONSTANTS.API_param_cartid, cartId);
params.put(CONSTANTS.API_param_token, Utils.getToken());
JSONObject postdata = new JSONObject(params);
try {
YupITApplication.getJsonWithHTTPPostResponse(params, mContext, 1, (id, jsonResult) -> {
if (jsonResult.getString(mContext.getString(R.string.status)).equalsIgnoreCase(mContext.getString(R.string.success))) {
itemCountPrice = jsonResult.getJSONObject("Data").getString("Count") + ","
+ jsonResult.getJSONObject("Data").getString("TotalPrice");
Log.e("itemCountPrice.............", "" + itemCountPrice);
// Here I get value
} else {
itemCountPrice = "0,0";
}
}, Utils.cartitemcount, postdata);
} catch (Exception e) {
e.printStackTrace();
}
} else {
Toast.makeText(mContext, mContext.getString(R.string.no_server_found), Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
return itemCountPrice;
// Here I get null and this is called before web service call
}
Utils.getitemCountPrice(cart_id));
每次我从上面的方法中得到null时,您都在混合异步调用和同步调用 当您调用方法getItemCountPrice时,您将实际从volley调用中收到您的值,而该值将从网络中可用。 您需要使用回调或未来调用 您可以像前面提到的那样使用回调进行操作:
public static String getitemCountPrice(String cartId, onDataListener pDataListener) {
// some code
YupITApplication.getJsonWithHTTPPostResponse(params, mContext, 1, (id, jsonResult) -> {
if (jsonResult.getString(mContext.getString(R.string.status)).equalsIgnoreCase(mContext.getString(R.string.success))) {
itemCountPrice = jsonResult.getJSONObject("Data").getString("Count") + ","
+ jsonResult.getJSONObject("Data").getString("TotalPrice");
Log.e("itemCountPrice.............", "" + itemCountPrice);
// Here I get value
} else {
itemCountPrice = "0,0";
}
//pass value through callback
pDataListener.onItemCountPriceReceived(itemCountPrice)
}, Utils.cartitemcount, postdata);
// some code
}
使用一个接口将数据传递回调用活动
interface OnDataListener{
void onItemCountPriceReceived(String itemCountPrice);
}
您的活动代码如下所示
Utils.getItemCountPrice(cart_id,new OnDataListener(){
@Override
void onItemCountPriceReceived(String itemCountPrice){
//you will get your value here when received from network call
}
})
public static void getitemCountPrice(String cartId, APIListener apiListener) {
try {
if (Utils.isNetworkAvailable(mContext)) {
HashMap<String, String> params = new HashMap<>();
params.put(CONSTANTS.API_param_cartid, cartId);
params.put(CONSTANTS.API_param_token, Utils.getToken());
JSONObject postdata = new JSONObject(params);
try {
YupITApplication.getJsonWithHTTPPostResponse(params, mContext, 1, (id, jsonResult) -> {
if (jsonResult.getString(mContext.getString(R.string.status)).equalsIgnoreCase(mContext.getString(R.string.success))) {
itemCountPrice = jsonResult.getJSONObject("Data").getString("Count") + ","
+ jsonResult.getJSONObject("Data").getString("TotalPrice");
Log.e("itemCountPrice.............", "" + itemCountPrice);
apiListener.onResponse(itemCountPrice);
// Here I get value
} else {
itemCountPrice = "0,0";
apiListener.onResponse(itemCountPrice);
}
}, Utils.cartitemcount, postdata);
} catch (Exception e) {
e.printStackTrace();
}
} else {
Toast.makeText(mContext, mContext.getString(R.string.no_server_found), Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
您正在混合异步调用和同步调用 当您调用方法getItemCountPrice时,您将实际从volley调用中收到您的值,而该值将从网络中可用。 您需要使用回调或未来调用 您可以像前面提到的那样使用回调进行操作:
public static String getitemCountPrice(String cartId, onDataListener pDataListener) {
// some code
YupITApplication.getJsonWithHTTPPostResponse(params, mContext, 1, (id, jsonResult) -> {
if (jsonResult.getString(mContext.getString(R.string.status)).equalsIgnoreCase(mContext.getString(R.string.success))) {
itemCountPrice = jsonResult.getJSONObject("Data").getString("Count") + ","
+ jsonResult.getJSONObject("Data").getString("TotalPrice");
Log.e("itemCountPrice.............", "" + itemCountPrice);
// Here I get value
} else {
itemCountPrice = "0,0";
}
//pass value through callback
pDataListener.onItemCountPriceReceived(itemCountPrice)
}, Utils.cartitemcount, postdata);
// some code
}
使用一个接口将数据传递回调用活动
interface OnDataListener{
void onItemCountPriceReceived(String itemCountPrice);
}
您的活动代码如下所示
Utils.getItemCountPrice(cart_id,new OnDataListener(){
@Override
void onItemCountPriceReceived(String itemCountPrice){
//you will get your value here when received from network call
}
})
public static void getitemCountPrice(String cartId, APIListener apiListener) {
try {
if (Utils.isNetworkAvailable(mContext)) {
HashMap<String, String> params = new HashMap<>();
params.put(CONSTANTS.API_param_cartid, cartId);
params.put(CONSTANTS.API_param_token, Utils.getToken());
JSONObject postdata = new JSONObject(params);
try {
YupITApplication.getJsonWithHTTPPostResponse(params, mContext, 1, (id, jsonResult) -> {
if (jsonResult.getString(mContext.getString(R.string.status)).equalsIgnoreCase(mContext.getString(R.string.success))) {
itemCountPrice = jsonResult.getJSONObject("Data").getString("Count") + ","
+ jsonResult.getJSONObject("Data").getString("TotalPrice");
Log.e("itemCountPrice.............", "" + itemCountPrice);
apiListener.onResponse(itemCountPrice);
// Here I get value
} else {
itemCountPrice = "0,0";
apiListener.onResponse(itemCountPrice);
}
}, Utils.cartitemcount, postdata);
} catch (Exception e) {
e.printStackTrace();
}
} else {
Toast.makeText(mContext, mContext.getString(R.string.no_server_found), Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return语句将在发出API调用后立即执行,不会等待响应,因为它是以同步方式编写的。您可以在界面的帮助下通知结果
interface APIListener{
public void onResponse(String itemPrice);
}
您的方法如下所示
Utils.getItemCountPrice(cart_id,new OnDataListener(){
@Override
void onItemCountPriceReceived(String itemCountPrice){
//you will get your value here when received from network call
}
})
public static void getitemCountPrice(String cartId, APIListener apiListener) {
try {
if (Utils.isNetworkAvailable(mContext)) {
HashMap<String, String> params = new HashMap<>();
params.put(CONSTANTS.API_param_cartid, cartId);
params.put(CONSTANTS.API_param_token, Utils.getToken());
JSONObject postdata = new JSONObject(params);
try {
YupITApplication.getJsonWithHTTPPostResponse(params, mContext, 1, (id, jsonResult) -> {
if (jsonResult.getString(mContext.getString(R.string.status)).equalsIgnoreCase(mContext.getString(R.string.success))) {
itemCountPrice = jsonResult.getJSONObject("Data").getString("Count") + ","
+ jsonResult.getJSONObject("Data").getString("TotalPrice");
Log.e("itemCountPrice.............", "" + itemCountPrice);
apiListener.onResponse(itemCountPrice);
// Here I get value
} else {
itemCountPrice = "0,0";
apiListener.onResponse(itemCountPrice);
}
}, Utils.cartitemcount, postdata);
} catch (Exception e) {
e.printStackTrace();
}
} else {
Toast.makeText(mContext, mContext.getString(R.string.no_server_found), Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public静态void getitemCountPrice(字符串cartId,apListener-apListener){
试一试{
if(Utils.isNetworkAvailable(mContext)){
HashMap params=新的HashMap();
参数put(CONSTANTS.API_param_cartid,cartid);
put(CONSTANTS.API_param_标记,Utils.getToken());
JSONObject postdata=新的JSONObject(参数);
试一试{
YupITApplication.getJsonWithHTTPPostResponse(参数,mContext,1,(id,jsonResult)->{
if(jsonResult.getString(mContext.getString(R.string.status)).equalsIgnoreCase(mContext.getString(R.string.success))){
itemCountPrice=jsonResult.getJSONObject(“数据”).getString(“计数”)+,“
+getJSONResult.getJSONObject(“数据”).getString(“总价”);
Log.e(“itemCountPrice………”,“+itemCountPrice”);
apistener.onResponse(itemCountPrice);
//在这里我得到了价值
}否则{
itemCountPrice=“0,0”;
apistener.onResponse(itemCountPrice);
}
},Utils.cartitemcount,postdata);
}捕获(例外e){
e、 printStackTrace();
}
}否则{
Toast.makeText(mContext、mContext.getString(R.string.no_server_found)、Toast.LENGTH_SHORT.show();
}
}捕获(例外e){
e、 printStackTrace();
}
}
您的return语句将在发出API调用后立即执行,不会等待响应,因为它是以同步方式编写的。您可以在界面的帮助下通知结果
interface APIListener{
public void onResponse(String itemPrice);
}
您的方法如下所示
Utils.getItemCountPrice(cart_id,new OnDataListener(){
@Override
void onItemCountPriceReceived(String itemCountPrice){
//you will get your value here when received from network call
}
})
public static void getitemCountPrice(String cartId, APIListener apiListener) {
try {
if (Utils.isNetworkAvailable(mContext)) {
HashMap<String, String> params = new HashMap<>();
params.put(CONSTANTS.API_param_cartid, cartId);
params.put(CONSTANTS.API_param_token, Utils.getToken());
JSONObject postdata = new JSONObject(params);
try {
YupITApplication.getJsonWithHTTPPostResponse(params, mContext, 1, (id, jsonResult) -> {
if (jsonResult.getString(mContext.getString(R.string.status)).equalsIgnoreCase(mContext.getString(R.string.success))) {
itemCountPrice = jsonResult.getJSONObject("Data").getString("Count") + ","
+ jsonResult.getJSONObject("Data").getString("TotalPrice");
Log.e("itemCountPrice.............", "" + itemCountPrice);
apiListener.onResponse(itemCountPrice);
// Here I get value
} else {
itemCountPrice = "0,0";
apiListener.onResponse(itemCountPrice);
}
}, Utils.cartitemcount, postdata);
} catch (Exception e) {
e.printStackTrace();
}
} else {
Toast.makeText(mContext, mContext.getString(R.string.no_server_found), Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public静态void getitemCountPrice(字符串cartId,apListener-apListener){
试一试{
if(Utils.isNetworkAvailable(mContext)){
HashMap params=新的HashMap();
参数put(CONSTANTS.API_param_cartid,cartid);
put(CONSTANTS.API_param_标记,Utils.getToken());
JSONObject postdata=新的JSONObject(参数);
试一试{
YupITApplication.getJsonWithHTTPPostResponse(参数,mContext,1,(id,jsonResult)->{
if(jsonResult.getString(mContext.getString(R.string.status)).equalsIgnoreCase(mContext.getString(R.string.success))){
itemCountPrice=jsonResult.getJSONObject(“数据”).getString(“计数”)+,“
+getJSONResult.getJSONObject(“数据”).getString(“总价”);
Log.e(“itemCountPrice………”,“+itemCountPrice”);
apistener.onResponse(itemCountPrice);
//在这里我得到了价值
}否则{
itemCountPrice=“0,0”;
apistener.onResponse(itemCountPrice);
}
},Utils.cartitemcount,postdata);
}捕获(例外e){
e、 printStackTrace();
}
}否则{
Toast.makeText(mContext、mContext.getString(R.string.no_server_found)、Toast.LENGTH_SHORT.show();
}
}捕获(例外e){
e、 printStackTrace();
}
}
你能发布一些日志吗?每次我在服务调用之前得到null和活动的函数调用,callu都需要在截击请求的成功回调中添加行“Utils.getitemCountPrice(cart_id));。你能发布一些日志吗?每次我在服务调用之前得到null和活动的函数调用,callu都需要添加行“Utils.getitemCountPrice(购物车id));“在截击请求的成功回调中。如何在调用util函数后获取值如何在调用util函数后获取值?”