gpt4 book ai didi

java - 从 onResponse 获取变量

转载 作者:搜寻专家 更新时间:2023-11-01 07:46:17 28 4
gpt4 key购买 nike

我想从 BDDRequest 类获取变量“response”,以便在我的 MainActivity 类的 ListView 中使用它,我该怎么做?

public class BDDRequest implements Serializable {
private final long serialVersionUID = 1L;

static private Activity activity;
public String req;
public BDDRequest(){}


public static void GetRequest(final Context t, UserEmployeeInfo User) {

activity = (Activity) t;


RequestQueue queue = Volley.newRequestQueue(t);
ParamsSend params = new ParamsSend();

params.setUser(User);
ParserJson<ParamsSend> pj = new ParserJson<>(params);


String strJson;
try {
strJson = pj.writeJSON();

} catch (JsonProcessingException e) {
strJson = "null";
}
final String data = strJson;
String REST_API_URL = "http://212.227.53.116:8080/WSmartgroom/rest/perso/request";
Log.d("lol", strJson);
StringRequest myReq = new StringRequest(Request.Method.PUT,
REST_API_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("reponse:", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("That didn't work!", "Error");
}
}) {
@Override
public String getBodyContentType() {
return "application/json";
}
@Override
public byte[] getBody() throws AuthFailureError {
return data.getBytes();
}
};
queue.add(myReq);
}
}

最佳答案

为它使用一个接口(interface),

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.io.Serializable;

public class BDDRequest implements Serializable {
private final long serialVersionUID = 1L;

static private Activity activity;
public String req;

public BDDRequest() {
}


public static void GetRequest(final Context t, UserEmployeeInfo User, final Callback callback) {

activity = (Activity) t;


RequestQueue queue = Volley.newRequestQueue(t);
ParamsSend params = new ParamsSend();

params.setUser(User);
ParserJson<ParamsSend> pj = new ParserJson<>(params);


String strJson;
try {
strJson = pj.writeJSON();

} catch (JsonProcessingException e) {
strJson = "null";
}
final String data = strJson;
String REST_API_URL = "http://212.227.53.116:8080/WSmartgroom/rest/perso/request";
Log.d("lol", strJson);
StringRequest myReq = new StringRequest(Request.Method.PUT,
REST_API_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("reponse:", response);
callback.onSuccess(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("That didn't work!", "Error");
callback.onError();
}
}) {
@Override
public String getBodyContentType() {
return "application/json";
}

@Override
public byte[] getBody() throws AuthFailureError {
return data.getBytes();
}
};
queue.add(myReq);
}


public interface Callback {
void onSuccess(String response);
void onError();
}

}

并在您的类上实现接口(interface)。

这样使用,

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;

import com.example.BDDRequest.Callback;

public class MainActivity extends FragmentActivity implements Callback {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BDDRequest.GetRequest(this, new UserEmployeeInfo(), this);


}

@Override
public void onSuccess(String response) {
// Bind the data to the listview
}

@Override
public void onError() {
//Show fallback message here
}
}

关于java - 从 onResponse 获取变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43635992/

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