我試圖使用他們的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;
}
}
'<!DOCTYPE ...'是不是JSON – SLaks
難道是他們的API服務器的兼容性問題? – SuicidalCookie