Skip to content

Instantly share code, notes, and snippets.

@Vagonn
Created November 18, 2018 17:19
Show Gist options
  • Save Vagonn/ccacd007e79d67f0e1195b7114de1793 to your computer and use it in GitHub Desktop.
Save Vagonn/ccacd007e79d67f0e1195b7114de1793 to your computer and use it in GitHub Desktop.
jsonParseExample
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.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
// diger kodlar
private void parseJSON(String url)
{
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.GET,
url,
null,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response)
{
try
{
progressBar.setVisibility(View.GONE);
JSONArray jsonArray = response.getJSONArray("data");
if (jsonArray.length() <= 0)
{
if (theme.equals("light"))
{
noDataTextView.setBackground(getResources().getDrawable(R.drawable.nodata_textview_background_light));
}
else
{
noDataTextView.setBackground(getResources().getDrawable(R.drawable.nodata_textview_background_dark));
}
noDataTextView.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
}
else
{
noDataTextView.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
for (int i = 0; i < jsonArray.length(); i++)
{
JSONObject data = jsonArray.getJSONObject(i);
String id = data.getString("id");
String date = data.getString("time");
String part = data.getString("part");
String team1 = data.getString("team1");
String team2 = data.getString("team2");
String leagueCode = data.getString("league_code");
if (!data.getString("score1").equals(""))
score1 = String.valueOf(data.getLong("score1"));
else
score1 = "-";
if (!data.getString("score2").equals(""))
score2 = String.valueOf(data.getLong("score2"));
else
score2 = "-";
if (team1.length() > 12)
team1 = team1.substring(0, 12) + ".";
if (team2.length() > 12)
team2 = team2.substring(0, 12) + ".";
// region Time
Long timestamp = data.getLong("time") * 1000;
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timestamp);
int monthNumber = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
if (monthNumber < 10)
monthText = "0" + monthNumber;
else
monthText = Integer.toString(monthNumber);
if (day < 10)
dayText = "0" + day;
else
dayText = Integer.toString(day);
date = dayText + "/" + monthText;
//endregion
System.out.println("PART: " + part);
// her xeberin melumatlarini bir-bir liste elave eliyir
calendarItemArrayList.add(new CalendarItem(id, team1, team2, date,
part, score1, score2, leagueCode));
}
}
calendarAdapter = new CalendarAdapter(CalendarActivity.this, calendarItemArrayList);
recyclerView.setAdapter(calendarAdapter);
calendarAdapter.setOnItemClickListener(CalendarActivity.this);
}
catch (JSONException e)
{
e.printStackTrace();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
error.printStackTrace();
progressBar.setVisibility(View.GONE);
Toast.makeText(CalendarActivity.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}
);
requestQueue.add(jsonObjectRequest);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment