2013-11-22 41 views
1

我試圖使用他們的json API解析4chan板列表。使用Android解析JSON使用Android

我試圖解析的JSON是這裏:http://a.4cdn.org/boards.json 我已經驗證了json,它似乎有效。

我得到這個錯誤:11-21 17:14:25.132: E/JSON Parser(32278): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

這裏是我的代碼:

package learn2crack.jsonparsing; 

    import org.json.JSONArray; 
    import org.json.JSONException; 
    import org.json.JSONObject; 
    import android.app.Activity; 
    import android.os.Bundle; 
    import android.widget.TextView; 

    import learn2crack.jsonparsing.library.JSONParser; 



    public class MainActivity extends Activity { 

//URL to get JSON Array 
private static String url = "http://a.4cdn.org/boards.json"; 

//JSON Node Names 
private static final String TAG_BOARDS = "boards"; 
private static final String TAG_BOARD = "board"; 
//private static final String TAG_WS_BOARD = "ws_board"; 
//private static final String TAG_EMAIL = "email"; 

JSONArray user = null; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 

    // Creating new JSON Parser 
    JSONParser jParser = new JSONParser(); 

    // Getting JSON from URL 
    JSONObject json = jParser.getJSONFromUrl(url); 

    try { 
     // Getting JSON Array 
     user = json.getJSONArray(TAG_BOARDS); 
     JSONObject c = user.getJSONObject(0); 

     // Storing JSON item in a Variable 
     String board = c.getString(TAG_BOARD); 
     //String name = c.getString(TAG_NAME); 
     //String email = c.getString(TAG_EMAIL); 

     //Importing TextView 
     final TextView uboard = (TextView)findViewById(R.id.board); 


     //Set JSON Data in TextView 
     uboard.setText(board); 



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



} 

}

而且,這裏是我的JSON解析器,我從一個教程了:

package learn2crack.jsonparsing.library; 

    import java.io.BufferedReader; 
    import java.io.IOException; 
    import java.io.InputStream; 
    import java.io.InputStreamReader; 
    import java.io.UnsupportedEncodingException; 

    import org.apache.http.HttpEntity; 
    import org.apache.http.HttpResponse; 
    import org.apache.http.client.ClientProtocolException; 
    import org.apache.http.client.methods.HttpPost; 
    import org.apache.http.impl.client.DefaultHttpClient; 
    import org.json.JSONException; 
    import org.json.JSONObject; 

    import android.util.Log; 

    public class JSONParser { 

static InputStream is = null; 
static JSONObject jObj = null; 
static String json = ""; 

// constructor 
public JSONParser() { 

} 

public JSONObject getJSONFromUrl(String url) { 

    // Making HTTP request 
    try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     is = httpEntity.getContent();   

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

    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     json = sb.toString(); 
    } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 

    // try parse the string to a JSON object 
    try { 
     jObj = new JSONObject(json); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

    // return JSON String 
    return jObj; 

} 

}

+0

'<!DOCTYPE ...'是不是JSON – SLaks

+0

難道是他們的API服務器的兼容性問題? – SuicidalCookie

回答

0

您正在獲取一些HTML代碼,而不是json。

更改您的getJSONFromUrl()方法是這樣的:

HttpClient client = new DefaultHttpClient(); 
HttpGet request = new HttpGet(url); 

HttpResponse response = client.execute(request); 


BufferedReader rd = new BufferedReader(
    new InputStreamReader(response.getEntity().getContent())); 

StringBuffer result = new StringBuffer(); 
String line = ""; 
while ((line = rd.readLine()) != null) { 
    result.append(line); 
} 
    return result.toString(); 
+0

未處理的異常類型爲IOException – SuicidalCookie

+0

行,所以我接着說:趕上(IOException異常E){ \t \t // ...處理錯誤...... \t \t} {終於 \t \t // ...清理將執行是否沒有發生錯誤... \t \t}'它給我這個錯誤 – SuicidalCookie

+0

@SuicidalCookie我也嘗試從本地主機獲取json數據,以便在我的android項目中查看,如果您已經解決了您的問題,可否請編輯您的上面的代碼,因爲我真的需要它爲我目前的項目。謝謝你。 – Hendy