2016-05-29 149 views
1

Process: com.example.venky.httpurl1, PID: 2578 java.lang.RuntimeException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 3 path $[0]错误:预期BEGIN_ARRAY但BEGIN_OBJECT

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    listView = (ListView) findViewById(R.id.rtlist); 
    client = new AsyncHttpClient(); 
    client.get(MainActivity.this, url, new com.loopj.android.http.AsyncHttpResponseHandler() { 
     @Override 
     public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody) { 
      String responsestr = new String(responseBody); 
      gson = new Gson(); 
      responseObj = gson.fromJson(responsestr, Response.class); 
      adapter = new CustomAdapter(MainActivity.this, responseObj); 
      listView.setAdapter(adapter); 
     } 

任何输入,请? 是因为我的JSON数据?

回答

0

检查responseBodyNULL或清空其转换为字符串,并解析它作为JSON值之前

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    listView = (ListView) findViewById(R.id.rtlist); 
    client = new AsyncHttpClient(); 
    client.get(MainActivity.this, url, new com.loopj.android.http.AsyncHttpResponseHandler() { 
     @Override 
     public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody) { 
      if(responseBody != null && responseBody.length > 0) { 
       String responsestr = new String(responseBody); 
       gson = new Gson(); 
       responseObj = gson.fromJson(responsestr, Response.class); 
       adapter = new CustomAdapter(MainActivity.this, responseObj); 
       listView.setAdapter(adapter); 
      } 
     } 
相关问题