GetCommitTask.java
2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package it.nexus.android.interview;
import android.content.Context;
import android.os.AsyncTask;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* Created by kel666 on 12/01/17.
*/
public class GetCommitTask extends AsyncTask<Void, Void, List<Commit>> {
private Context context;
public GetCommitTask(Context context) {
this.context=context;
}
@Override
protected List<Commit> doInBackground(Void... voids) {
Vector<Commit> commits=new Vector<Commit>();
InputStream is = context.getResources().openRawResource(R.raw.commits);
try {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
String jsonString = writer.toString();
JSONArray arr=new JSONArray(jsonString);
for(int i=0; i<arr.length(); i++) {
JSONObject o=arr.getJSONObject(i);
Commit c=new Commit();
c.setId(o.getString("id"));
c.setTitle(o.getString("title"));
c.setAuthor(o.getString("author_name"));
c.setMsg(o.getString("message"));
SimpleDateFormat parser=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
try {
c.setDate(parser.parse(o.getString("created_at")));
} catch (ParseException e) {
e.printStackTrace();
}
commits.add(c);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return commits;
}
}