android


Stuck with JSONAPI implementation


So I'm working with the format over at jsonapi.org and I just keep getting null in my fields. No idea what I'm doing wrong.
Here's a snippet of the JSON I'm trying to parse (it doesn't have the include filed at all)
{
"jsonapi": {
"version": "1.0"
},
"meta": {
"page": "1",
"pages": "59",
"records": "588",
"records_per_page": "10",
"keywords": "paint"
},
"data": [
{
"id": "2619621",
"type": "TalkThread",
"attributes": {
"name": "Cracked paint",
"created": "Thu Apr 21 2016",
"thread_view": "121",
"first_nick": "Rupster"
},
"links": {
"self": "2619621-Cracked-paint"
},
"relationships": {
"posts": {
"meta": {
"records": "9"
},
"data": [
{
"id": "60601161",
"type": "TalkPost",
"attributes": {
"text": "The emulsion I applied on to a wall has cracked.I know I can solve the problem by applying a base...",
"nick": "Rupster"
}
},
{
"id": "60619191",
"type": "TalkPost",
"attributes": {
"text": "There was paint on the wall before I painted it. I don't know...",
"nick": "Rupster"
}
},
{
"id": "60619901",
"type": "TalkPost",
"attributes": {
"text": "The old paint may well have been silk. I noticed the same i...",
"nick": "Rupster"
}
}
]
},
"topic": {
"data": {
"id": "2750",
"type": "Topic",
"attributes": {
"name": "Property/DIY"
},
"links": {
"self": "property"
}
}
}
},
"site": {
"name": "site"
}
}
I've built one model class which isn't working as I intended to.
SearchTalkThread.java
import com.gustavofao.jsonapi.Annotations.Type;
import com.gustavofao.jsonapi.Models.Resource;
/**
* Created by bilalhaider on 15/05/2017.
*/
#Type("TalkThread")
public class SearchTalkThread extends Resource {
public String name;
public String created;
public String thread_view;
public String first_nick;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCreated() {
return created;
}
public void setCreated(String created) {
this.created = created;
}
public String getThread_view() {
return thread_view;
}
public void setThread_view(String thread_view) {
this.thread_view = thread_view;
}
public String getFirst_nick() {
return first_nick;
}
public void setFirst_nick(String first_nick) {
this.first_nick = first_nick;
}
}
Now I'm using github user's faogustavo's JSONApi library (link) but it just isn't going to plan. I'll get the ID, Type and Link but that's about it.
My request is:
NewGsonRequest.java
import android.support.annotation.Nullable;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonRequest;
import com.google.gson.Gson;
import com.gustavofao.jsonapi.JSONApiConverter;
import com.gustavofao.jsonapi.Models.JSONApiObject;
import com.gustavofao.jsonapi.Models.Resource;
import com.mumsnet.talk.common.Constants;
import com.mumsnet.talk.model.search.SearchTalkThread;
import com.mumsnet.talk.model.search.TalkPost;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;
/**
* Created by bilalhaider on 08/05/2017.
*/
public class NewGsonRequest<T> extends JsonRequest<T> {
private final Gson gson = new Gson();
private Map<String, String> bodyJson;
private final Class<T> clazz;
private Map<String, String> headers;
private Response.Listener listener;
private static final String CONTENT_TYPE =
String.format("application/x-www-form-urlencoded");
;
public NewGsonRequest(String url, Class<T> clazz, Map<String, String> headers, int method, #Nullable Map<String, String> bodyJson,
Response.Listener<T> listener, Response.ErrorListener errorListener) {
super(method, url, getFormDataString(bodyJson), listener, errorListener);
this.clazz = clazz;
this.listener = listener;
this.bodyJson = bodyJson;
if (headers == null) {
this.headers = new HashMap<>();
} else {
this.headers = headers;
}
this.headers.put("client-id", Constants.API_CLIENT_ID);
this.headers.put("client_secret", Constants.API_CLIENT_SECRET);
// this.headers.put(CONTENT_TYPE, "application/json; charset=utf-8");
this.headers.put("Accept-Encoding", "gzip, deflate, sdch");
}
private static String getFormDataString(Map<String, String> formData) {
StringBuilder params = new StringBuilder();
if (formData != null) {
for (String key : formData.keySet()) {
params.append("&").append(key).append("=").append(formData.get(key));
}
return params.toString().substring(1);
} else {
return null;
}
}
#Override
public Map<String, String> getParams() throws AuthFailureError {
return bodyJson != null ? bodyJson : super.getParams();
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers != null ? headers : super.getHeaders();
}
#Override
public String getBodyContentType() {
return CONTENT_TYPE;
}
#Override
protected void deliverResponse(T response) {
listener.onResponse(response);
}
#Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
JSONApiConverter api = new JSONApiConverter(SearchTalkThread.class, TalkPost.class);
JSONApiObject object = new JSONApiObject();
String json = null;
String encoding = response.headers.get("Content-Encoding");
if (encoding != null && encoding.equals("gzip")) {
json = unpackData(response.data);
object = api.fromJson(json);
} else {
try {
json = new String(
response.data, HttpHeaderParser.parseCharset(response.headers));
object = api.fromJson(json);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
if (object.getData().size() > 0) {
if (object.getData().size() == 1) {
SearchTalkThread searchTalkThread = (SearchTalkThread) object.getData(0);
} else {
List<Resource> resources = object.getData();
}
}
return Response.success(gson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response));
}
private String unpackData(byte[] data) {
String output = "";
try {
GZIPInputStream gStream = new GZIPInputStream(new ByteArrayInputStream(data));
InputStreamReader reader = new InputStreamReader(gStream);
BufferedReader in = new BufferedReader(reader);
String read;
while ((read = in.readLine()) != null) {
output += read;
}
reader.close();
in.close();
gStream.close();
} catch (IOException e) {
return null;
}
return output;
}
}
Can anyone help me as to where I need to go or what I need to do? It has been stressing me out for a few days and I just can't figure out what it is I'm doing wrong!!
Thanks in advance
Data which you are trying to fetch is dynamic (coming from webservice) or static (json file included in project)?
I have gone through the link which you have provided for JSONAPI, in that code there is MainActivity where code for fetching data is there , can you add your code for the same.

Related Links

android : non zero exit value 2 after Use Google Analytics
how to add swipe left and on image click drawer Android
Can't start with AVD emulator for Galaxy Note 4 (5.0.1)
CoordinatorLayout with CollapsingToolbarLayout not displaying the large toolbar
Receive SMS broadcasts in Android 6.0+ [duplicate]
How do I access all available event color keys that are available in the CalendarContract.EventColumn?
Android TranslateAnimation not smooth when a videoview is in the same layout
ViewPager with PagerAdapter in Fragment disapper when onRusume
Display locally saved picture with Cordova
Only first cookie gets on server, several others cookies don't
Issue in setting Images with dynamic columns in grid view
how to load different image to different button without creating multiple database in android studio?
Android DownloadManager not deleting the physical file when remove is called
Generic parser with gson (key and content dynamic attributes)
Why TAB swipe reload content?
FireBase - Two devices should be triggered at the same time

Categories

HOME
url-redirection
asp.net-web-api
xml
fabricjs
gridview
c++11
ng-idle
pagination
themes
apache-spark-mllib
ms-access-2016
opendj
aws-cli
jquery-ui
banner
lanczos
discrete-mathematics
extjs4.2
pcl-crypto
mel
onedrive-api
chargify
easyphp
template-engine
google-container-registry
mapstruct
delphi-xe5
lilypond
kendo-mobile
scriptella
interpreter
dynamically-generated
xcode-ui-testing
siri
pylons
has-and-belongs-to-many
bringtofront
openmdao
realm-mobile-platform
calculation
constexpr
aws-codecommit
code-snippets
keras-layer
reactjs.net
indy10
processwire
release
node-mssql
diacritics
typesafe-config
jvmti
vmware-fusion
claims
puredata
httrack
linter
corruption
er-diagram
oozie-coordinator
cd-burning
ampps
branch-prediction
javafx-css
restore
atlas
winusb
dotnetnuke-7
rspec2
drawstring
lean
shopping
freepbx
aurora
nupic
realsense
design-principles
concept-insights
character-replacement
dynamics-nav-2016
computer-architecture
pypiserver
poppler
rubaxa-sortable
.bash-profile
iced-coffeescript
viola-jones
ms-access-2000
xmltype
iiop
big-ip
strawberry-perl
raygun
trdion2011
build-script
mintty
reflector
cancellation
square-bracket
conditionaltagsupport
scriptaculous
breakout
inappsettingskit
cmmi
flex++
merb
device-width
mkv
oracle-enterprise-linux
android-holo-everywhere
mysql-error-1205
unattended-processing
instantiationexception
drupal-fivestar
rowlex
murmurhash
xpsdocument
httpcookie

Resources

Encrypt Message