Skip to content

Commit

Permalink
Parsing with gson
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinmarnold committed Aug 24, 2015
1 parent 808cb53 commit d4cd70c
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 210 deletions.
1 change: 1 addition & 0 deletions app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
</content>
<orderEntry type="jdk" jdkName="Android API 22 Platform (1)" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="gson-2.3.1" level="project" />
<orderEntry type="library" exported="" name="library-1.0.18" level="project" />
<orderEntry type="library" exported="" name="support-v4-22.2.1" level="project" />
<orderEntry type="library" exported="" name="support-annotations-22.2.1" level="project" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@

import org.stingraymappingproject.api.clientandroid.ClientService;
import org.stingraymappingproject.api.clientandroid.RecurringRequest;
import org.stingraymappingproject.api.clientandroid.models.Factoid;
import org.stingraymappingproject.api.clientandroid.requesters.FactoidsRequester;
import org.stingraymappingproject.api.clientandroid.requesters.Requester;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {
Expand Down Expand Up @@ -88,10 +91,15 @@ public void onServiceConnected(ComponentName name, IBinder service) {
mStingrayClientService = ((ClientService.ClientServiceBinder) service).getService();
mBoundToStingrayClientService = true;

Requester factoidsRequester = new FactoidsRequester(mStingrayClientService) {
Requester<Factoid[]> factoidsRequester = new FactoidsRequester(mStingrayClientService) {

@Override
public void onResponse(Object response) {
public void onResponse(Factoid[] response) {
List<Factoid> factoids = Arrays.asList(response);
Log.d(TAG, "onResponse");
for(Factoid f : factoids) {
Log.d(TAG, f.getFact());
}
}

@Override
Expand Down
1 change: 1 addition & 0 deletions clientandroid/stingray-api-client.iml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
</content>
<orderEntry type="jdk" jdkName="Android API 22 Platform (1)" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="gson-2.3.1" level="project" />
<orderEntry type="library" exported="" name="library-1.0.18" level="project" />
</component>
</module>
1 change: 1 addition & 0 deletions stingray-api-client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ android {

dependencies {
compile 'com.mcxiaoke.volley:library:1.0.18'
compile 'com.google.code.gson:gson:2.3.1'
}

apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import com.android.volley.VolleyError;

import org.json.JSONObject;
import org.stingraymappingproject.api.clientandroid.params.RequestParams;
import org.stingraymappingproject.api.clientandroid.requesters.JsonObjectResponseRequester;
import org.stingraymappingproject.api.clientandroid.requesters.Requester;

/**
* Created by Marvin Arnold on 23/08/15.
Expand All @@ -32,8 +31,8 @@ public void testRequestsPointToFullApiUrl() {
ClientService client = getService();
client.setApiBaseUrl(apiBaseUrl);

TestJsonObjectResponseRequester r = new TestJsonObjectResponseRequester(client);
assertEquals(apiBaseUrl + apiEndpoint1, r.testGetRequestParams().getRequestUrl());
TestRequester r = new TestRequester(client);
assertEquals(apiBaseUrl + apiEndpoint1, r.getRequest().getUrl());
}

public void testInitializes() {
Expand All @@ -43,34 +42,30 @@ public void testInitializes() {
assertTrue(client.isInitialized);
}

class TestJsonObjectResponseRequester extends JsonObjectResponseRequester {
class TestRequester extends Requester {

public TestJsonObjectResponseRequester(ClientService clientService) {
public TestRequester(ClientService clientService) {
super(clientService);
}

@Override
protected JSONObject getJSONObject() {
return null;
public void onErrorResponse(VolleyError error) {

}

@Override
protected RequestParams getRequestParams() {
return getRequestParams(apiEndpoint1, Request.Method.GET);
}
public void onResponse(Object response) {

public RequestParams testGetRequestParams() {
return getRequestParams();
}

@Override
public void onErrorResponse(VolleyError error) {

protected JSONObject getJSONObjectParameters() {
return null;
}

@Override
public void onResponse(Object response) {

protected GsonRequest getRequest() {
return getRequest(apiEndpoint1, Request.Method.GET, null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

import org.stingraymappingproject.api.clientandroid.params.RequestParams;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -44,7 +42,7 @@ public RequestQueue getRequestQueue() {
private List<RecurringRequest> mRecurringRequests;

private RequestQueue mRequestQueue;
private ArrayList<RequestParams> mOfflineRequests;
private ArrayList<GsonRequest> mOfflineRequests;
private ConnectivityManager mConnectivityManager;
private ScheduledExecutorService mRequestScheduler;
public boolean isInitialized = false;
Expand Down Expand Up @@ -76,16 +74,16 @@ public int onStartCommand(Intent intent, int flags, int startId) {
}

public void queueOfflineRequests() {
Log.d(TAG, "queueOfflineRequests");
if(isInitialized && isOnline()) {
ArrayList<RequestParams> t = (ArrayList<RequestParams>) mOfflineRequests.clone();
mOfflineRequests.clear();

for(RequestParams offlineRequest : t) {
Log.d(TAG, "Syncing offlineRequests");
mRequestQueue.add(offlineRequest.buildRequest());
}
}
// Log.d(TAG, "queueOfflineRequests");
// if(isInitialized && isOnline()) {
// ArrayList<GsonRequest> t = (ArrayList<GsonRequest>) mOfflineRequests.clone();
// mOfflineRequests.clear();
//
// for(GsonRequest offlineRequest : t) {
// Log.d(TAG, "Syncing offlineRequests");
// mRequestQueue.add(offlineRequest.buildRequest());
// }
// }
}

public boolean isOnline() {
Expand All @@ -100,7 +98,7 @@ public void init() {
mRequestScheduler = Executors.newScheduledThreadPool(1);
mRecurringRequests = new ArrayList<>();
mRequestQueue = Volley.newRequestQueue(mContext);
mOfflineRequests = new ArrayList<RequestParams>();
mOfflineRequests = new ArrayList<GsonRequest>();
this.mConnectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
this.isInitialized = true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.stingraymappingproject.api.clientandroid;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

/**
* Created by Marvin Arnold on 23/08/15.
*/
public class GsonRequest<T> extends Request<T> {
public JSONObject getJsonObject() {
return mRequestJsonObject;
}
public Response.Listener getSuccessListener() {
return mSuccessListener;
}

protected final Gson mGson;

/** class of type of response */
protected final Class<T> mClazz;
private final JSONObject mRequestJsonObject;
private final Response.Listener mSuccessListener;

public GsonRequest(JSONObject jsonObject,
String requestUrl,
int method,
Response.Listener<T> successListener,
Response.ErrorListener errorListener,
Class<T> responseClazz) {
super(method, requestUrl, errorListener);

this.mRequestJsonObject = jsonObject;
this.mSuccessListener = successListener;
this.mGson = new Gson();
this.mClazz = responseClazz;
}

@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
String json = new String(
response.data, HttpHeaderParser.parseCharset(response.headers));
return Response.success(
mGson.fromJson(json, mClazz), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JsonSyntaxException e) {
return Response.error(new ParseError(e));
}
}

@Override
protected void deliverResponse(T response) {
this.mSuccessListener.onResponse(response);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.stingraymappingproject.api.clientandroid.models;

/**
* Created by Marvin Arnold on 24/08/15.
*/
public class Factoid {
String id;

public String getFact() {
return fact;
}

String fact;
String created_at;
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit d4cd70c

Please sign in to comment.