我正在尝试从我的网站获取 JSON 文件并对其进行解析。不幸的是,到目前为止还没有运气。我能够使用另一个名为 BaslikContainer 的类来获取数据,但是,我需要这段代码在另一个类中工作,这需要在 arraylist 中的 Baslik 实例,所以我需要删除它,现在我不能让它工作。
有人可以指出这个问题吗?没有抛出异常 - 我不知道为什么会这样。
package info.funstein.TestHttpGet;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class TestHttpGet {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception{
BufferedReader in = null;
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("http://dev.funstein.info/baslik.json"));
HttpResponse response = client.execute(request);
in = new BufferedReader
(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String page = sb.toString();
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
Basliklar basliklar = gson.fromJson(page, Basliklar.class);
System.out.println(basliklar);
}
}
class Basliklar {
private List<Baslik> basliklar;
@Override
public String toString() {
return String.format("[Basliklar: %1$s]", basliklar);
}
}
class Baslik {
private String baslikAdi;
private String baslikAcan;
private int baslikID;
public String getBaslikAdi() {
return baslikAdi;
}
public void setBaslikAdi(String baslikAdi) {
this.baslikAdi = baslikAdi;
}
public String getBaslikAcan() {
return baslikAcan;
}
public void setBaslikAcan(String baslikAcan) {
this.baslikAcan = baslikAcan;
}
public int getBaslikID() {
return baslikID;
}
public void setBaslikID(int baslikID) {
this.baslikID = baslikID;
}
@Override
public String toString() {
return String.format("[Baslik: baslikAdi=%1$s, baslikAcan=%2$s, baslikID=%3$d]",
baslikAdi, baslikAcan, baslikID);
}
}