坚持使用JSONAPI实现

时间:2017-05-15 20:21:30

标签: android json json-api

所以我在jsonapi.org处理格式,我只是在我的字段中保持为空。不知道我做错了什么。

这是我试图解析的JSON的片段(它根本没有提交包含)

    {
  "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"
      }
    }

我已经建立了一个不按我的意图工作的模型类。

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;
    }


}

现在我正在使用github用户的faogustavo的JSONApi库(link),但它还没有计划好。我会得到ID,类型和链接但是关于它。

我的要求是:

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;
    }
}

任何人都可以帮助我找到我需要去的地方或我需要做什么吗?它一直在向我强调几天,我只是无法弄清楚我做错了什么!

编辑添加了用于获取JSON的其他类。我遇到的问题是在获取数据后解析数据。

SearchFeedDataFactory.java

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.mumsnet.talk.common.Constants;
import com.mumsnet.talk.request.GsonRequest;
import com.mumsnet.talk.request.NewGsonRequest;
import com.mumsnet.talk.response.SearchResponse;
import com.mumsnet.talk.utils.PreferenceConnector;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by bilalhaider on 03/05/2017.
 */

public class SearchFeedDataFactory {

    private Context mContext;

    public SearchFeedDataFactory(Context context) {
        mContext = context;
    }

    public void searchThreads(String keywords, SearchFeedCallback callback) {

        Map<String, String> accessHeader = new HashMap<>();
        accessHeader.put("Authorization", "Bearer "
                + PreferenceConnector.readString(mContext, "authToken"));

        String searchURL = Constants.BASE_URL + "api/v1/forums/threads/search?keywords=" + keywords
                +"&page=1&per_page=25";

        final SearchFeedCallback searchFeedCallback = callback;

        NewGsonRequest<SearchResponse> searchRequest = new NewGsonRequest<>(searchURL, SearchResponse.class,
                accessHeader, Request.Method.GET, null, new Response.Listener<SearchResponse>() {
            @Override
            public void onResponse(SearchResponse response) {

                searchFeedCallback.onSearchDataReceived(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                searchFeedCallback.onSearchDataFailed(error);

            }
        });

        RequestFactory.getInstance(mContext).addtoRequestQueue(searchRequest);

    }

    public interface SearchFeedCallback {
        void onSearchDataReceived(SearchResponse response);
        void onSearchDataFailed(Exception exception);
    }


}

SearchResponse.java

import com.google.gson.annotations.SerializedName;
import com.mumsnet.talk.model.search.SearchTalkThread;

import java.util.ArrayList;

/**
 * Created by bilalhaider on 03/05/2017.
 */
public class SearchResponse {

    @SerializedName("data")
    public ArrayList<SearchTalkThread> searchDataList;

    public SearchResponse() {
        searchDataList = new ArrayList<>();
    }

    public SearchTalkThread getItem(int index) {
        return searchDataList.size() - 1 >= index ? searchDataList.get(index) : null;
    }

    public ArrayList<SearchTalkThread> getItems() {
        return searchDataList;
    }

}

提前致谢

2 个答案:

答案 0 :(得分:1)

而不是使用api.fromJson尝试下面的代码来解析Json。

public static final String JSON_ARRAY = "data";
public static final String KEY_ID = "id";
public static final String KEY_NAME = "name";
public static final String KEY_EMAIL = "link";

protected void parseJSON(){
    JSONObject jsonObject=null;
    try {
        jsonObject = new JSONObject(json);
        users = jsonObject.getJSONArray(JSON_ARRAY);

        ids = new String[users.length()];
        names = new String[users.length()];
        emails = new String[users.length()];

        for(int i=0;i<users.length();i++){
            JSONObject jo = users.getJSONObject(i);
            ids[i] = jo.getString(KEY_ID);
            names[i] = jo.getString(KEY_NAME);
            emails[i] = jo.getString(KEY_EMAIL);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

只是检查你是否得到了值。

答案 1 :(得分:0)

您尝试获取的数据是动态的(来自Web服务)还是静态的(项目中包含的json文件)?

我已经浏览了您为JSONAPI提供的链接,在该代码中有MainActivity,其中有用于获取数据的代码,是否可以添加相同的代码。