gpt4 book ai didi

java - 如何创建单个 volley webservice 类以在 Android 应用程序中使用?

转载 作者:行者123 更新时间:2023-12-02 05:02:47 25 4
gpt4 key购买 nike

我的 Android 应用程序在单个 Activity 中使用多个网络调用,并且我有几个这样的 Activity ,我必须同时使用 post 和 get 请求。我想创建一个“VolleyWebservice”类并在多个 Activity 中调用相同的类,而不是编写完整的 volley 代码。我对 Android 开发比较陌生,我不明白我哪里出了问题。

public class VolleyWebService {

public JSONObject result;

public JSONObject getResponse(String url, Context mContext) {
RequestQueue mQueue = Volley.newRequestQueue(mContext);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e(TAG, "Anshuman" + response.toString());

result = response;
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});

mQueue.add(request);
return result;
}
}

我的 Activity 中调用此类的方法

    private void callFunctionGetDist() {
ProgressDialog progressDialog;
progressDialog = ProgressDialog.show(recorddata.this, "", "Please Wait...", true);
JSONObject response = new VolleyWebService().getResponse(urlConfigClass.GET_DISTRICT, this);
try {
if(response.toString().contains("Status:Success,Details")){
arrDistName.clear();
arrDistCode.clear();
arrDistName.add("Select District Name");
arrDistCode.add("Select District Code");
JSONArray jsonArray = response.getJSONArray("Status:Success,Details");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jobJ = jsonArray.getJSONObject(i);
String scheName = jobJ.getString("post");
JSONObject jobJDist = new JSONObject(scheName);
String distname = jobJDist.getString("District");
String distcode = jobJDist.getString("DistrictCode");
arrDistName.add(distname);
arrDistCode.add(distcode);
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(recorddata.this,
R.layout.custom_textview_to_spinner, arrDistName);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
district.setAdapter(dataAdapter);
progressDialog.dismiss();
}else{
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Response is null or empty", Toast.LENGTH_LONG).show();
}
} catch (Exception volleyError) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), volleyError.getMessage(), Toast.LENGTH_LONG).show();
}
}

我尝试创建相同的类(class),但我无法获得对其他 Activity 的响应。虽然我在 Volley jsonbject 响应中得到了正确的响应,但在其他 Activity 中响应返回 null。

我想让结果对象在我的记录数据 Activity 中返回响应

这是我到目前为止所尝试过的,但没有运气!

  public void postResponse (String url, Context mContext, final VolleyResponseListener listener) {
try {
String encodedUrl = url.replace(" ", "%20") + "";
if (encodedUrl.contains("("))
encodedUrl = encodedUrl.replace("(", "%28");
if (encodedUrl.contains(")"))
encodedUrl = encodedUrl.replace(")", "%29");
encodedUrl = encodedUrl.replace(" ", "%20");

RequestQueue mQueue = Volley.newRequestQueue(mContext);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POst, encodedUrl, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e(TAG, "Anshuman" + response.toString());

listener.onSuccess(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
listener.onError(error);
}
}) {
@Override
protected Map<String, String> getParams() {
return new HashMap<>();
}

};

mQueue.add(request);
} catch (Exception e) {
e.printStackTrace();
}
}

最佳答案

为了实现你的目标,(不考虑架构和编码原则),你可以传递一个回调:

public class VolleyWebService {

public interface VolleyResponseListener {
void onSuccess(JSONObject response);
void onError(VolleyError error);
}

public JSONObject result;

public JSONObject getResponse(String url, Context mContext, VolleyResponseListener listener) {
RequestQueue mQueue = Volley.newRequestQueue(mContext);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e(TAG, "Anshuman" + response.toString());

listener.onSuccess(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
listener.onError(error);
}
});

mQueue.add(request);
return result;
}
}

使用它:

volleyWebService.getResponse("your url", context, new VolleyResponseListener() {
@Override
void onSuccess(JSONObject response) {
//do what you want on success
}

@Override
void onError(VolleyError error) {
//do what you want on error
}
});

关于java - 如何创建单个 volley webservice 类以在 Android 应用程序中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56377370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com