在Android上使用Retrofit 2获取JSONException

时间:2016-10-30 06:05:04

标签: android nullpointerexception retrofit2

我正在使用Fabric.io Twitter Kit。创建了一个自定义服务类,并使用Retrofit转换该类中的接口。底部的代码(CustomTwitterApiClient类)。

我得到NullPointerException,如果我抓住了,我得到JSONException。从日志中可以看出,我可以获得趋势响应,但我无法阅读它。如果我尝试readLine()并将其附加到StringBuilder,我会null

我在此SO answer的指导下编写了代码。

我做错了什么?

提前致谢。

PS:那些额外的异常处理程序只是为了获得一个干净的日志。

    D/LOG-DEBUG: Line 86 : [{"trends":[{"name":"#Worlds","url":"http:\/\/twitter.com\/search?q=%23Worlds","promoted_content":null,"query":"%23Worlds","tweet_volume":99974},{"name":"#LaNocheDeML","url":"http:\/\/twitter.com\/search?q=%23LaNocheDeML","promoted_content":null,"query":"%23LaNocheDeML","tweet_volume":56697},{"name":"#CLEMVSFSU","url":"http:\/\/twitter.com\/search?q=%23CLEMVSFSU","promoted_content":null,"query":"%23CLEMVSFSU","tweet_volume":10016},{"name":"#\u0634\u064A_\u062A\u062D\u0628_\u062A\u0634\u0645\u0647","url":"http:\/\/twitter.com\/search?q=%23%D8%B4%D9%8A_%D8%AA%D8%AD%D8%A8_%D8%AA%D8%B4%D9%85%D9%87","promoted_content":null,"query":"%23%D8%B4%D9%8A_%D8%AA%D8%AD%D8%A8_%D8%AA%D8%B4%D9%85%D9%87","tweet_volume":52441},{"name":"#DiosdadoEsPueblo","url":"http:\/\/twitter.com\/search?q=%23DiosdadoEsPueblo","promoted_content":null,"query":"%23DiosdadoEsPueblo","tweet_volume":44538},{"name":"Juanma","url":"http:\/\/twitter.com\/search?q=Juanma","promoted_content":null,"query":"Juanma","tweet_volume":13642},{"name":"Vince Vaughn","url":"http:\/\/twitter.com\/search?q=%22Vince+Vaughn%22","promoted_content":null,"query":"%22Vince+Vaughn%22","tweet_volume":10409},{"name":"Florida State","url":"http:\/\/twitter.com\/search?q=%22Florida+State%22","promoted_content":null,"query":"%22Florida+State%22","tweet_volume":10936},{"name":"Francois","url":"http:\/\/twitter.com\/search?q=Francois","promoted_content":null,"query":"Francois","tweet_volume":24324},{"name":"HOJE TEM TWD","url":"http:\/\/twitter.com\/search?q=%22HOJE+TEM+TWD%22","promoted_content":null,"query":"%22HOJE+TEM+TWD%22","tweet_volume":null},{"name":"Atividade Paranormal 4","url":"http:\/\/twitter.com\/search?q=%22Atividade+Paranormal+4%22","promoted_content":null,"query":"%22Atividade+Paranormal+4%22","tweet_volume":null},{"name":"BELIEBERS VOTANDO","url":"http:\/\/twitter.com\/search?q=%22BELIEBERS+VOTANDO%22","promoted_content":null,"query":"%22BELIEBERS+VOTANDO%22","tweet_volume":56259},{"name":"South Carolina","url":"http:\/\/twitter.com\/search?q=%22South+Carolina%22","promoted_content":null,"query":"%22South+Carolina%22","tweet_volume":16795},{"name":"Deshaun Watson","url":"http:\/\/twitter.com\/search?q=%22Deshaun+Watson%22","promoted_content":null,"query":"%22Deshaun+Watson%22","tweet_volume":12224},{"name":"Soulja Boy","url":"http:\/\/twitter.com\/search?q=%22Soulja+Boy%22","promoted_content":null,"query":"%22Soulja+Boy%22","tweet_volume":196642},{"name":"\uB864\uB4DC\uCEF5","url":"http:\/\/twitter.com\/search?q=%EB%A1%A4%EB%93%9C%EC%BB%B5","promoted_content":null,"query":"%EB%A1%A4%EB%93%9C%EC%BB%B5","tweet_volume":null},{"name":"Racing","url":"http:\/\/twitter.com\/search?q=Racing","promoted_content":null,"query":"Racing","tweet_volume":69655},{"name":"Jimbo","url":"http:\/\/twitter.com\/search?q=Jimbo","promoted_content":null,"query":"Jimbo","tweet_volume":null},{"name":"Silver Scrapes","url":"http:\/\/twitter.com\/search?q=%22Silver+Scrapes%22","promoted_content":null,"query":"%22Silver+Scrapes%22","tweet_volume":null},{"name":"#\u0627\u0646\u0627_\u0645\u0648\u0627\u0637\u0646_\u0645\u062B\u0644\u064A_\u0645\u062B\u0644\u0643\u0645","url":"http:\/\/twitter.com\/search?q=%23%D8%A7%D9%86%D8%A7_%D9%85%D9%88%D8%A7%D8%B7%D9%86_%D9%85%D8%AB%D9%84%D9%8A_%D9%85%D8%AB%D9%84%D9%83%D9%85","promoted_content":null,"query":"%23%D8%A7%D9%86%D8%A7_%D9%85%D9%88%D8%A7%D8%B7%D9%86_%D9%85%D8%AB%D9%84%D9%8A_%D9%85%D8%AB%D9%84%D9%83%D9%85","tweet_volume":23899},{"name":"#\u0627\u0637\u0631\u062F\u0648_\u0627\u0635\u062D\u0627\u0628_\u0627\u0644\u0642\u0647\u0648\u0647_\u0627\u0644\u0633\u0648\u062F\u0627\u0621","url":"http:\/\/twitter.com\/search?q=%23%D8%A7%D8%B7%D8%B1%D8%AF%D9%88_%D8%A7%D8%B5%D8%AD%D8%A7%D8%A8_%D8%A7%D9%84%D9%82%D9%87%D9%88%D9%87_%D8%A7%D9%84%D8%B3%D9%88%D8%AF%D8%A7%D8%A1","promoted_content":null,"query":"%23%D8%A7%D8%B7%D8%B1%D8%AF%D9%88_%D8%A7%D8%B5%D8%AD%D8%A7%D8%A8_%D8%A7%D9%84%D9%82%D9%87%D9%88%D9%87_%D8%A7%D9%84%D8%B3%D9%88%D8%AF%D8%A7%D8%A1","tweet_volume":null},{"name":"#\u0627\u0646\u0627_\u0645\u062F\u0627\u0648\u0
    D/LOG-DEBUG: Line 111: 
    W/System.err: org.json.JSONException: End of input at character 0 of 
    W/System.err:     at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
    W/System.err:     at org.json.JSONTokener.nextValue(JSONTokener.java:97)
    W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:92)
    W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:108)
    W/System.err:     at .MainActivity$1$1.onResponse(MainActivity.java:113)
    W/System.err:     at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)

此代码阻止错误指向:

@Override
public void success(Result<TwitterSession> result) {
    // The TwitterSession is also available through:
    // Twitter.getInstance().core.getSessionManager().getActiveSession()
    TwitterSession session = result.data;
    // TODO: Remove toast and use the TwitterSession's userID
    // with your app's user model
    String msg = "@" + session.getUserName() + " logged in! (#" + session.getUserId() + ")";
    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();


    CustomTwitterApiClient customTwitterApiClient = new CustomTwitterApiClient(session);
    Call<ResponseBody> cevap = customTwitterApiClient.getCustomService().show(1);
    cevap.enqueue(new retrofit2.Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            BufferedReader reader = null;
            StringBuilder stringBuilder2 = new StringBuilder();


            try {
                reader = new BufferedReader(new InputStreamReader(response.body().byteStream()));
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                Log.d("READER", "86 : reader.readLine() : "+reader.readLine());

            } catch (IOException e) {
                e.printStackTrace();
            }

            String line= "";

            try{
                while ((line = reader.readLine())!= null)
                {

                    try {
                        stringBuilder2.append(line);
                    } catch (NullPointerException e) {
                        e.printStackTrace();
                    }

                }
            }
            catch(IOException e){
                e.printStackTrace();
            }

            try {
               Log.d("READER", "111: stringBuilder2.toString(): " +stringBuilder2.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }


            try {
                JSONArray sonucJsonArray = new JSONArray(stringBuilder2.toString());
                JSONObject sonucJsonObject = sonucJsonArray.getJSONObject(0);
                JSONArray trendsJsonArray = sonucJsonObject.getJSONArray("trends");

                JSONObject trendJson;

                for(int i=0; i < trendsJsonArray.length() ; i++){
                    TrendTopic trend = new TrendTopic();
                    trendJson = trendsJsonArray.getJSONObject(i);

                    trend.set_id(i+1);
                    trend.set_trendName(trendJson.getString("name"));
                    trend.set_trendQuery(trendJson.getString("query"));
                    trend.set_trendURL(trendJson.getString("url"));

                    trendTopics.add(i,trend);

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {

        }
    });

这是CustomTwitterApiClient类:

public class CustomTwitterApiClient extends TwitterApiClient {

    public CustomTwitterApiClient(TwitterSession session) {
        super(session);
    }

    public CustomService getCustomService() {
        return getService(CustomService.class);
    }
}

interface CustomService {
    @GET("/1.1/trends/place.json")
    Call<ResponseBody> show(@Query("id") int id);
}

0 个答案:

没有答案