Issue
I am getting data from web service using volley library but I want to move to another activity when I get the data by volley response so I want to wait until i have connection response. When I used AsyncTask
, and put the transfer function in post execution, the second activity start the the data received as I saw in log cat. and when I put the transfer function inside volley OnResponse
the activity freezing. So how can wait until get response and save the data then move to another activity to use it.
Here is the code:
public class Loading_Car_info extends AppCompatActivity {
String appUrl="https://darialkndari5.000webhostapp.com/read_info.php";
String user_id,name,v_date,v_no,qr_code;
String[] phaths;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading__car_info);
Bundle b=this.getIntent().getExtras();
MyAsyncTask m=new MyAsyncTask();
m.execute();
}
public void transfer()
{
Bundle b=new Bundle();
b.putStringArray("paths", phaths);
b.putString("name","hi");
b.putString("v_dat","hello");
b.putString("v_no","rrr");
b.putString("qr_code",qr_code);
Intent intent=new Intent(this,User_Info.class);
intent.putExtras(b);
startActivity(intent);
}
public class MyAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
//UI Interaction
}
@Override
protected Void doInBackground(Void... params) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, appUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
boolean move=false;
phaths=new String[response.length()];
try {
JSONObject jsnobject = new JSONObject(response);
JSONArray jsonArray = jsnobject.getJSONArray("ads");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject student = jsonArray.getJSONObject(i);
v_date=student.getString("v_date");
v_no=student.getString("v_no");
qr_code=student.getString("Qr_code");
name=student.getString("name");
phaths[i]=student.getString("path");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("MainActivity 2" + error.toString());
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("id", "3");
return params;
}
};
NotificationData.getmInstance(Loading_Car_info.this).addrequestqueue(stringRequest);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
transfer();
}
}
}
Solution
You need to call transfer after getting all your data within onResponse()
:
try {
JSONObject jsnobject = new JSONObject(response);
JSONArray jsonArray = jsnobject.getJSONArray("ads");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject student = jsonArray.getJSONObject(i);
v_date=student.getString("v_date");
v_no=student.getString("v_no");
qr_code=student.getString("Qr_code");
name=student.getString("name");
phaths[i]=student.getString("path");
}
transfer();
} catch (JSONException e) {
e.printStackTrace();
}
Answered By - HaroldSer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.