hope you all are fine . I am developing weather app in android where android app get data from server in json format. In log file you can see that i successfully get my json data using volley, now i want to display that data in list view. How i will do that ???
package com.example.the_weather_forecast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> tubeLines = new ArrayList<String>();
String URL = "xyz";
RequestQueue requestQueue = Volley.newRequestQueue(this);
JsonArrayRequest getRequest = new JsonArrayRequest(Request.Method.GET, URL, null,
new Response.Listener<JSONArray>()
{
@Override
public void onResponse(JSONArray response) {
// display response
Log.d("Response", response.toString());
for(int i=0;i<response.length();i++){
try {
JSONArray jsonArray = response.getJSONArray(i);
String x = jsonArray.toString();
tubeLines.add(x);
System.out.println("reached" + x);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", error.toString());
}
}
);
getRequest.setRetryPolicy(new RetryPolicy() {
@Override
public int getCurrentTimeout() {
return 50000;
}
@Override
public int getCurrentRetryCount() {
return 50000;
}
@Override
public void retry(VolleyError error) throws VolleyError {
}
});
ListView myListView = (ListView)findViewById(R.id.myListview);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, tubeLines);
myListView.setAdapter(arrayAdapter);
// add it to the RequestQueue
requestQueue.add(getRequest);
}
}
Log File : This my json data which i am receiving from my server
D/Response: [[10,10,10,10,15,17,13,11],[12,8,8,8,9,9,14,10],[0,0,0,0,0,0,0,0]]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…