Java 使用改装发送POST请求并接收Json响应无法使用响应数据我需要将其传递给其他活动
我使用改型发送POST请求。服务器返回是一个JSON响应,我能够在回调方法中解析响应。我需要将数据从服务器传递到另一个活动。但是我不能在外面使用响应数据Java 使用改装发送POST请求并接收Json响应无法使用响应数据我需要将其传递给其他活动,java,android,json,server,retrofit,Java,Android,Json,Server,Retrofit,我使用改型发送POST请求。服务器返回是一个JSON响应,我能够在回调方法中解析响应。我需要将数据从服务器传递到另一个活动。但是我不能在外面使用响应数据 api.LoginUser( Email.getText().toString(), // passing value to interface of retrofit Password.getText().toString(), new Call
api.LoginUser(
Email.getText().toString(), // passing value to interface of retrofit
Password.getText().toString(),
new Callback<Response>() {
@Override
public void success(Response result, Response response) {
BufferedReader reader = null;
String output = "";
try {
reader = new BufferedReader(new InputStreamReader(result.getBody().in()));
output = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
//Json PArsing
try {
JSONObject mainObject = new JSONObject(output);
JSONObject dataObj = mainObject.getJSONObject("data");
String id = dataObj.getString("id");
String name = dataObj.getString("name");
n=name;
Log.d("jsontext", n); //This works
}
catch(JSONException e)
{
e.printStackTrace();
}
Toast.makeText(MainActivity.this, output, Toast.LENGTH_LONG).show();
}
@Override
public void failure(RetrofitError error) {
//If any error occured displaying the error as toast
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
}
);
您可以创建一个对象并实现可序列化的
:
class User implements Serializable {
...
}
然后将对象User
放入bundle,添加到intent:
Bundle bundle = new Bundle();
bundle.putSerializable("data", user);
Intent intent = new Intent(this, YourClass.class);
intent.putExtras(bundle);
startActivity(intent);
希望它对您有所帮助。将所有数据保存在一个字符串中,并使用intent app另一个活动对其进行解析 您可以按如下方式操作
成功时公共无效(int statusCode,Header[]headers,
字节[]响应){
之后
公共void showStoreData(字符串st){
您应该使用从调用方法初始化的接口,并将其作为参数传递到请求类中,这样您就可以从任何位置调用请求,并将回调响应返回到调用它的位置,例如:
通用接口,在另一个文件中分开:
public interface SomeCustomListener<T>
{
public void getResult(T object);
}
公共接口自定义侦听器
{
public void getResult(T对象);
}
在课堂上等待你的电话(完成你需要的东西):
public void someRequestReturningString(对象参数1,最终SomeCustomListener侦听器)
{
//在这里你初始化你需要的…这是你的东西
response.enqueue(新回调()
{
@凌驾
public void onResponse(Call-Call,2.Response-rawResponse)
{
尝试
{
字符串响应=rawResponse.body().String();
//用它做你想做的,并以此为基础。。。
//将其返回给调用此方法的用户
getResult(“someResultString”);
}
捕获(例外e)
{
e、 printStackTrace();
getResult(“Error1…”);
}
}
@凌驾
失败时公共无效(调用、可丢弃)
{
尝试
{
//如果发生错误,请执行其他操作
getResult(“Error2…”);
}
捕获(例外e)
{
printStackTrace();
getResult(“Error3…”);
}
}
});
}
然后从调用请求的位置(可以是任何位置、片段、onclick等):
公共类BlaBla
{
//.....
公共方法()
{
NetworkManager.getInstance().someRequestReturningString(someObject,new SomeCustomListener())
{
@凌驾
public void getResult(字符串结果)
{
如果(!result.isEmpty())
{
//用结果做你需要的。。。
}
}
});
}
}
如果您需要更多上下文,可以参考
希望这能有所帮助!使用jsontext的日志在使用outer的日志时给出了名称-它给了我一个错误-printf需要打印一个值-我想Log.d(“outer”,n);
错误在这里。检查n
是否不是null
。n在回调函数中保留名称的值它的作用域存在于函数之外post the logcat than请在您的logcat.thanx中发布您的答案,但无法在回调函数中实现意图thanx用于响应,该值仅可用在回调函数中,我无法将其存储到全局变量中。它不起作用。我知道您想将对象发送到另一个活动?
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(new ByteArrayInputStream(
response)));
String st = "";
String st1 = "";
while ((st = br.readLine()) != null) {
st1 = st1 + st;
}
showStoreData(st1);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] errorResponse, Throwable e) {
// called when response HTTP status is "4XX" (eg. 401, 403, 404)
Log.e("FAIL", "FAIl" + statusCode);
}
@Override
public void onRetry(int retryNo) {
// called when request is retried
}
});
Intent intent = new Intent(this, YourClass.class);
intent.putExtras(st);
startActivity(intent);
}
public interface SomeCustomListener<T>
{
public void getResult(T object);
}
public void someRequestReturningString(Object param1, final SomeCustomListener<String> listener)
{
//here you initialize what you need... it's your stuff
response.enqueue(new Callback<ResponseBody>()
{
@Override
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> rawResponse)
{
try
{
String response = rawResponse.body().string();
// do what you want with it and based on that...
//return it to who called this method
listener.getResult("someResultString");
}
catch (Exception e)
{
e.printStackTrace();
listener.getResult("Error1...");
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable throwable)
{
try
{
// do something else in case of an error
listener.getResult("Error2...");
}
catch (Exception e)
{
throwable.printStackTrace();
listener.getResult("Error3...");
}
}
});
}
public class BlaBla
{
//.....
public void someMethod()
{
NetworkManager.getInstance().someRequestReturningString(someObject, new SomeCustomListener<String>()
{
@Override
public void getResult(String result)
{
if (!result.isEmpty())
{
//do what you need with the result...
}
}
});
}
}