我正在使用Retrofit发送POST请求。服务器返回一个JSON响应,我能够在回调方法中解析响应。我需要将数据从服务器传递到另一个活动。但我不能在外面使用响应数据。
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();
}
}
);
我执行App crashes时不能使用它。现在ok.varow中没有值来从回调中获取值响应OnSuccess方法???
Log.d("outer",n);
Intent dash = new Intent(this,Dashboard.class);
dash.putExtra("Value",fi);
startActivity(dash);
}
答案 0 :(得分:0)
您可以创建一个对象并实现Serializable
:
class User implements Serializable {
...
}
然后将对象User
置于捆绑包中,添加到intent:
Bundle bundle = new Bundle();
bundle.putSerializable("data", user);
Intent intent = new Intent(this, YourClass.class);
intent.putExtras(bundle);
startActivity(intent);
希望对你有所帮助。
答案 1 :(得分:0)
将所有数据保存在字符串中并使用intent app另一个活动并解析它;
答案 2 :(得分:0)
您可以按照以下方式执行此操作
public void onSuccess(int statusCode,Header [] headers, byte [] response){
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
}
});
之后
public void showStoreData(String st){
Intent intent = new Intent(this, YourClass.class);
intent.putExtras(st);
startActivity(intent);
}
答案 3 :(得分:0)
您应该使用从调用方法初始化的接口,并将其作为参数传递到您的请求类中,这样您就可以从任何地方调用请求并将回调响应返回到您调用它的位置,例如是:
一般界面,在另一个文件中分隔:
UIView.animateWithDuration(10.0, animations: {
textField.becomeFirstResponder()
})
在接听电话的班上(完成你需要的东西):
public interface SomeCustomListener<T>
{
public void getResult(T object);
}
然后从你调用请求的地方(可以是任何地方,片段,onClicks等):
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...");
}
}
});
}
如果您需要更多上下文,可以参考this SO thread。
希望这有帮助!