来自Google Books API的JSON Object NullPointerException

时间:2014-08-02 10:59:19

标签: java android json barcode-scanner google-books

我在使用JSON对象从Google Books API检索书名等信息时遇到了一些麻烦。 以下是相关代码:

//Retrieves scanned data from barcode reader
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
    if (scanningResult != null) {
        String scanContent = scanningResult.getContents();
        String scanFormat = scanningResult.getFormatName();

        if(scanContent!=null && scanFormat!=null && scanFormat.equalsIgnoreCase("EAN_13")){
            String bookSearchString = "https://www.googleapis.com/books/v1/volumes?" +
                    "q=isbn:"+scanContent+"&key=564967988072"; //my key
            new GetBookInfo().execute(bookSearchString);

            String booktitle = booktitleText.getText().toString();
            BookGenActivity.setBookTitle(booktitle);

        }
        else{
                Toast toast = Toast.makeText(getApplicationContext(), 
                    "Not a valid scan!", Toast.LENGTH_SHORT);
                    toast.show();
        }           

    }
    else{
        Toast noData = Toast.makeText(getApplicationContext(), 
            "No scan data received!", Toast.LENGTH_SHORT);
        noData.show();
    }

}

以下是获取图书信息的课程:

    private class GetBookInfo extends AsyncTask<String, Void, String> {
    //fetch book info
    @Override
    protected String doInBackground(String... bookURLs) {
    //request book info
        StringBuilder bookBuilder = new StringBuilder();
        for (String bookSearchURL : bookURLs) {
            //search urls
            HttpClient bookClient = new DefaultHttpClient();
            try {
                //get the data
                HttpGet bookGet = new HttpGet(bookSearchURL);
                HttpResponse bookResponse = bookClient.execute(bookGet);
                StatusLine bookSearchStatus = bookResponse.getStatusLine();
                if (bookSearchStatus.getStatusCode()==200) {
                    //we have a result
                    HttpEntity bookEntity = bookResponse.getEntity();
                    InputStream bookContent = bookEntity.getContent();
                    InputStreamReader bookInput = new InputStreamReader(bookContent);
                    BufferedReader bookReader = new BufferedReader(bookInput);
                    String lineIn;
                    while ((lineIn=bookReader.readLine())!=null) {
                        bookBuilder.append(lineIn);
                    }

                }

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

        }
        return bookBuilder.toString();

    }

    protected void onPostExecute(String result) {
        //parse search results
        try{
            //parse results
            JSONObject resultObject = new JSONObject(result);
            JSONArray bookArray = resultObject.getJSONArray("items");
            JSONObject bookObject = bookArray.getJSONObject(0);
            JSONObject volumeObject = bookObject.getJSONObject("volumeInfo");

            try{ 
                bookTextView.setText("TITLE: "+volumeObject.getString("title"));

            }
            catch(JSONException jse){ 
                booktitleText.setText("Not Found");
                jse.printStackTrace(); 
            }
        }
        //If no result is found
        catch (Exception e) {
            //no result
            e.printStackTrace();
            bookTextView.setText("NOT FOUND");
        }

    }

}

我扫描条形码后似乎有一个NullPointerException,然后应用程序崩溃,但我找不到原因。下面是堆栈跟踪:

08-02 11:34:56.171: W/System.err(6994): org.json.JSONException: End of input at character 0 of 
08-02 11:34:56.171: W/System.err(6994):     at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
08-02 11:34:56.171: W/System.err(6994):     at org.json.JSONTokener.nextValue(JSONTokener.java:97)
08-02 11:34:56.171: W/System.err(6994):     at org.json.JSONObject.<init>(JSONObject.java:155)
08-02 11:34:56.171: W/System.err(6994):     at org.json.JSONObject.<init>(JSONObject.java:172)
08-02 11:34:56.171: W/System.err(6994):     at com.myapps.bscanr.BookActivity$GetBookInfo.onPostExecute(BookActivity.java:128)
08-02 11:34:56.171: W/System.err(6994):     at com.myapps.bscanr.BookActivity$GetBookInfo.onPostExecute(BookActivity.java:1)
08-02 11:34:56.171: W/System.err(6994):     at android.os.AsyncTask.finish(AsyncTask.java:632)
08-02 11:34:56.171: W/System.err(6994):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
08-02 11:34:56.171: W/System.err(6994):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
08-02 11:34:56.171: W/System.err(6994):     at android.os.Handler.dispatchMessage(Handler.java:102)
08-02 11:34:56.171: W/System.err(6994):     at android.os.Looper.loop(Looper.java:136)
08-02 11:34:56.171: W/System.err(6994):     at android.app.ActivityThread.main(ActivityThread.java:5001)
08-02 11:34:56.171: W/System.err(6994):     at java.lang.reflect.Method.invokeNative(Native Method)
08-02 11:34:56.171: W/System.err(6994):     at java.lang.reflect.Method.invoke(Method.java:515)
08-02 11:34:56.171: W/System.err(6994):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-02 11:34:56.171: W/System.err(6994):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-02 11:34:56.171: W/System.err(6994):     at dalvik.system.NativeStart.main(Native Method)
08-02 11:34:56.171: D/AndroidRuntime(6994): Shutting down VM
08-02 11:34:56.171: W/dalvikvm(6994): threadid=1: thread exiting with uncaught exception (group=0x4159dba8)
08-02 11:34:56.171: E/AndroidRuntime(6994): FATAL EXCEPTION: main
08-02 11:34:56.171: E/AndroidRuntime(6994): Process: com.myapps.bscanr, PID: 6994
08-02 11:34:56.171: E/AndroidRuntime(6994): java.lang.NullPointerException
08-02 11:34:56.171: E/AndroidRuntime(6994):     at com.myapps.bscanr.BookActivity$GetBookInfo.onPostExecute(BookActivity.java:168)
08-02 11:34:56.171: E/AndroidRuntime(6994):     at com.myapps.bscanr.BookActivity$GetBookInfo.onPostExecute(BookActivity.java:1)
08-02 11:34:56.171: E/AndroidRuntime(6994):     at android.os.AsyncTask.finish(AsyncTask.java:632)
08-02 11:34:56.171: E/AndroidRuntime(6994):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
08-02 11:34:56.171: E/AndroidRuntime(6994):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
08-02 11:34:56.171: E/AndroidRuntime(6994):     at android.os.Handler.dispatchMessage(Handler.java:102)
08-02 11:34:56.171: E/AndroidRuntime(6994):     at android.os.Looper.loop(Looper.java:136)
08-02 11:34:56.171: E/AndroidRuntime(6994):     at android.app.ActivityThread.main(ActivityThread.java:5001)
08-02 11:34:56.171: E/AndroidRuntime(6994):     at java.lang.reflect.Method.invokeNative(Native Method)
08-02 11:34:56.171: E/AndroidRuntime(6994):     at java.lang.reflect.Method.invoke(Method.java:515)
08-02 11:34:56.171: E/AndroidRuntime(6994):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-02 11:34:56.171: E/AndroidRuntime(6994):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-02 11:34:56.171: E/AndroidRuntime(6994):     at dalvik.system.NativeStart.main(Native Method)

非常感谢任何关于此的指导!

0 个答案:

没有答案