主にJavaScriptとFlashで記述されたLinkedInアプリを作成していますが、すべてのデータはJavaプロキシから取得されています。データはJSONである必要がありますが、残念ながらLinkedInはXMLのみをサポートしています。最善の解決策は、クライアントに送り返す前にサーバー上でXMLをJSONに変換することですが、確かに私のJavaスキルはそれほど強力ではありません。動作するように見えるコードがありますが、JSONObject例外が発生します。
org.jsonパッケージを使用してXMLを操作しています:http://json.org/java/
これは、XMLをJSONに変換しようとするJavaスニペットです。きれいではありませんが、データの変換を少し進めようとしています。
public static String readResponse(HttpResponse response) {
System.out.println("Reading response...");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String readLine;
String innhold = "";
while (((readLine = br.readLine()) != null)) {
innhold += readLine;
}
try {
JSONObject myJ = new JSONObject();
String ret = myJ.getJSONObject(innhold).toString();
System.out.println(ret);
return ret;
} catch (Exception e) {
System.out.println(e);
}
return innhold;
} catch (IOException e) {
System.out.println(e);
return null;
}
}
これが私が変換しようとしているものと非常によく似たデータです:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
<first-name>First</first-name>
<last-name>Last</last-name>
<headline>My Profile</headline>
<site-standard-profile-request>
<url>http://www.linkedin.com/profile</url>
</site-standard-profile-request>
</person>
そして、これが私が得ている例外です:
org.json.JSONException: JSONObject["<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><person> <first-name>First<\/first-name> <last-name>Last<\/last-name> <headline>My Profile<\/headline> <site-standard-profile-request> <url>http://www.linkedin.com/profile<\/url> <\/site-standard-profile-request><\/person>"] not found.
どんな助けでもありがたいです、ありがとう!