Hey you can retrieve your data using your methods, that depends on you. For example, I have never used a third party library to retrieve data from the server.
Think about this: a class that I can name FileContentReader
with a method getContentFromUrl
this will fetch your JSON data as string that you can then parse using JSONObject
or JSONArray
according to your file structure.
public class FileContentReader {
private Context appContext;
public FileContentReader(Context context){
this.appContext=context;
}
public String getContentFromUrl(String url)
{
StringBuilder content = new StringBuilder();
try {
URL u = new URL(url);
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
if (uc.getResponseCode()==HttpURLConnection.HTTP_OK) {
InputStream is = uc.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String line;
while ((line = br.readLine()) != null) {
content.append(line).append("\n");
}
}else{
throw new IOException(uc.getResponseMessage());
}
} catch(StackOverflowError | Exception s){
s.printStackTrace();
} catch(Error e){
e.printStackTrace();
}
return content.toString();
}
}
You can use the code this way inside an asynchronous task or any background task :
FileContentReader fcr= new FileContentReader(getApplicationContext());
String data= fcr.getContentFromUrl("myurl");
if(!data.isEmpty())
{
try{
JSONArray ja = new JSONArray(data);
//... ou can then access and manupilate your data the way you want
}catch(JSONException e)
{
e.printStackTrace();}
}