在Android客户端中读取JSON响应的异常

时间:2014-03-25 21:34:00

标签: android json web-services rest

获取读取JSON响应的异常/错误:

这是我从服务器返回的响应:

{
  "message":"Your request was successful!",
  "searchResults": [
    "Result 1 google",
    "Result 2 amazon",
     "Result 3 ebay"
  ],
  "status":200
}

方法#1按照以下说明进行操作:http://developer.android.com/reference/android/util/JsonReader.html

代码:

public class JsonResponseReader {

    /*reads input stream of the http JSON response & returns list of YourMarketNetResponse, 
     * calls readMessagesArray(JsonReader reader)*/
    public List<YourMarketNetResponse> readJsonStream(InputStream in) throws IOException {
         JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
         try {
             return readMessagesArray(reader);
         }
          finally {
           reader.close();
           reader = null; 

         }

       }//end 
/*
 * Reads using JSON reader & returns  List<YourMarketNetResponse>, uses readMessage(JsonReader reader) helper */
 List<YourMarketNetResponse> readMessagesArray(JsonReader reader) throws IOException {
         List<YourMarketNetResponse> messages = new ArrayList<YourMarketNetResponse>();

         //loop to go through JSON messages 
         reader.beginArray();
         while (reader.hasNext()) {
           messages.add(readMessage(reader));
         }
         reader.endArray();
         return messages;
       }//end 
/*
 *  reads  Reads using JSON reader & returns YourMarketNetResponse object looking specifically for "searchResults" key/value within JSON*/

       public YourMarketNetResponse readMessage(JsonReader reader) throws IOException {

         YourMarketNetResponse newYourMarketNetResponse = null; 
         //loop to go through each JSON message
         reader.beginObject();
         while (reader.hasNext()) {

             //if searchResults key 
           if (reader.nextName().equals("searchResults") && reader.peek() != JsonToken.NULL) 
           {
               //call search results String array  
               newYourMarketNetResponse = new  YourMarketNetResponse();
               newYourMarketNetResponse.setSearchResults(stringArray(reader));
           }   
           else
           {
             //skip everything else 
             reader.skipValue();

           }

         }//end JSON object loop 
          reader.endObject();


         return  newYourMarketNetResponse;
       }//end readMessage(JsonReader reader)

       public List<String> stringArray(JsonReader reader) throws IOException
       {
          List <String> searchResults = new ArrayList<String>();
         //loop to go through array within JSON object 
           reader.beginArray();
         while (reader.hasNext())
         {

             searchResults.add(reader.nextString());

         }
         reader.endArray();

         return searchResults;
       }

}//end class JsonResponseReader

我写了以下代码:

错误:

03-25 14:06:47.386: W/System.err(19965): java.lang.IllegalStateException: Expected BEGIN_ARRAY but was    BEGIN_OBJECT….

上面的错误实际上是正确的,没有发送的对象数组,它只是一个对象,所以如果在JSON字符串中遇到,我改变了读取对象或数组的方法,参见方法#2

方法#2 代码:

package com.yourmarketnet.yourmarketapp.netio;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import com.yourmarketnet.yourmarketapp.YourMarketNetResponse;

import android.util.JsonReader;
import android.util.JsonToken;

public class JsonResponseReader {

    /*reads input stream of the http JSON response & returns list of YourMarketNetResponse, 
     * calls readMessagesArray(JsonReader reader)*/
    public List<YourMarketNetResponse> readJsonStream(InputStream in) throws IOException {
         JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
         try {
             return readMessagesArray(reader);
         }
          finally {
           reader.close();
           reader = null; 

         }

       }//end 
/*
 * Reads using JSON reader & returns  List<YourMarketNetResponse>, uses readMessage(JsonReader reader) helper */
 List<YourMarketNetResponse> readMessagesArray(JsonReader reader) throws IOException {
         List<YourMarketNetResponse> messages = new ArrayList<YourMarketNetResponse>();

         //loop to go through JSON messages 
         //reader.beginArray();
         reader.beginObject();
         while (reader.hasNext()) {
           messages.add(readMessage(reader));
         }
         //reader.endArray();
         reader.endObject();
         return messages;
       }//end 
/*
 *  reads  Reads using JSON reader & returns YourMarketNetResponse object looking specifically for "searchResults" key/value within JSON*/

       public YourMarketNetResponse readMessage(JsonReader reader) throws IOException {

         YourMarketNetResponse newYourMarketNetResponse = null; 
         //loop to go through each JSON message
         //reader.beginObject();
         //while (reader.hasNext()) {

             //if searchResults key 
           if (reader.peek() !=null && reader.peek() != JsonToken.NULL && reader.nextName().equals("searchResults") ) 
           {
               //call search results String array  
               newYourMarketNetResponse = new  YourMarketNetResponse();
               newYourMarketNetResponse.setSearchResults(stringArray(reader));
           }   
           else
           {
             //skip everything else 
             reader.skipValue();

           }

         //}//end JSON object loop 
          //reader.endObject();


         return  newYourMarketNetResponse;
       }//end readMessage(JsonReader reader)

       public List<String> stringArray(JsonReader reader) throws IOException
       {
          List <String> searchResults = new ArrayList<String>();
         //loop to go through array within JSON object 
           reader.beginArray();
         while (reader.hasNext())
         {

             searchResults.add(reader.nextString());

         }
         reader.endArray();

         return searchResults;
       }

}//end class JsonResponseReader

错误:

03-25 14:52:26.597: W/System.err(24538): Unexpected token END OF FILE at position 0.
03-25 14:52:26.597: W/System.err(24538):    at org.json.simple.parser.JSONParser.parse(JSONParser.java:257)....

我怎么读这个/我做错了什么? 感谢

0 个答案:

没有答案