Greetins的编码员! 我搜索了论坛,但似乎无法找到我需要的答案。我是一个Android应用程序初学者,所以可以大大使用一些帮助! 我有一个从服务器获取的API,我正在尝试反序列化它(使用GSon)并用它填充我的列表视图。我的应用程序下载API但由于某种原因它将gson返回为null,因为我尝试反序列化它。 我创建了一类getter(Company.java),对它的响应(Response.java) - [不确定它的结构是否正确]我在MainActivity中调用LoadJsonTask.java。 谁能给我一些指示?我非常感谢! (对不起,有这么多代码,但想让我的问题容易理解:)) 谢谢!
这是我的代码: 公司类:
public class Company {
private String id;
private String name;
private String phone;
private String address;
private String postcode;
private String gps_lat;
private String gps_lng;
private String summary;
private String website;
private String facebook;
private String twitter;
private String webform;
private String email;
public String getId(){
return id;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public String getAddress() {
return address;
}
public String getPostcode() {
return postcode;
}
public String getGps_lat() {
return gps_lat;
}
public String getGps_lng(){
return gps_lng;
}
public String getSummary(){
return summary;
}
public String getWebsite(){
return website;
}
public String getFacebook(){
return facebook;
}
public String getTwitter(){
return twitter;
}
public String getWebform(){
return webform;
}
public String getEmail(){
return email;
}
}
我的回复课程: 公共课回复{
private List<Company> companies = new ArrayList<Company>();
public List<Company> getCompanies() {
return companies;
}
}
加载JSonTask:
public class LoadJSONTask extends AsyncTask<String, Void, Response> {
public LoadJSONTask(Listener listener) {
mListener = listener;
}
public interface Listener {
void onLoaded(List<Company> androidList);
void onError();
}
private Listener mListener;
@Override
protected Response doInBackground(String... strings) {
try {
String stringResponse = loadJSON(strings[0]);
List<Company> android = new Gson().fromJson(stringResponse, new TypeToken<ArrayList<Company>>(){}.getType());
return android;
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (JsonSyntaxException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(Response response) {
if (response != null) {
mListener.onLoaded(response.getCompanies());
} else {
mListener.onError();
}
}
private String loadJSON(String jsonURL) throws IOException {
URL url = new URL(jsonURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = in.readLine()) != null) {
response.append(line);
}
in.close();
return response.toString();
}
}
MainActivity类:
public class MainActivity extends AppCompatActivity implements LoadJSONTask.Listener, AdapterView.OnItemClickListener {
private ListView mListView;
public static final String URL = "url_with_api";
private List<HashMap<String, String>> mAndroidMapList = new ArrayList<>();
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PHONE = "phone";
private static final String KEY_ADDRESS = "address";
private static final String KEY_POSTCODE = "postcode";
private static final String KEY_GPS_LAT = "gps_lat";
private static final String KEY_GPS_LNG = "gps_lng";
private static final String KEY_SUMMARY = "summary";
private static final String KEY_WEBSITE = "website";
private static final String KEY_FACEBOOK = "facebook";
private static final String KEY_TWITTER = "twitter";
private static final String KEY_WEBFORM = "webform";
private static final String KEY_EMAIL = "email";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.list_view);
mListView.setOnItemClickListener(this);
new LoadJSONTask(this).execute(URL);
}
@Override
public void onLoaded(List<Company> androidList) {
for (Company android : androidList) {
HashMap<String, String> map = new HashMap<>();
map.put(KEY_ID, android.getId());
map.put(KEY_NAME, android.getName());
map.put(KEY_PHONE, android.getPhone());
map.put(KEY_ADDRESS, android.getAddress());
map.put(KEY_POSTCODE, android.getPostcode());
map.put(KEY_GPS_LAT, android.getGps_lat());
map.put(KEY_GPS_LNG, android.getGps_lng());
map.put(KEY_SUMMARY, android.getSummary());
map.put(KEY_WEBSITE, android.getWebsite());
map.put(KEY_FACEBOOK, android.getFacebook());
map.put(KEY_TWITTER, android.getTwitter());
map.put(KEY_WEBFORM, android.getWebform());
map.put(KEY_EMAIL, android.getEmail());
mAndroidMapList.add(map);
}
loadListView();
}
@Override
public void onError() {
Toast.makeText(this, "Error !", Toast.LENGTH_SHORT).show();
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(this, mAndroidMapList.get(i).get(KEY_NAME),Toast.LENGTH_LONG).show();
}
private void loadListView() {
ListAdapter adapter = new SimpleAdapter(MainActivity.this, mAndroidMapList, R.layout.list_item,
new String[] { KEY_ID, KEY_NAME, KEY_PHONE },
new int[] { R.id.version,R.id.name, R.id.api });
mListView.setAdapter(adapter);
}
}
我下载的API:
[{"id":4,"name":"Name","phone":"Phone","address":"","postcode":"","gps_lat":null,"gps_lng":null,"summary":"Des.","website":"http:","facebook":"","twitter":"","webform":"","email":"Email"},{"id":8,"name":"Name2","phone":"Phone2","address2":"","postcode":"","gps_lat":null,"gps_lng":null,"summary":"Des2.","website":"http:","facebook":"","twitter":"","webform":"","email":"Email"}]
答案 0 :(得分:1)
JSON是Company
数组而不是对象{companies:[]}
这意味着您必须使用以下内容反序列化JSON:
Company[] companies = new Gson().fromJson(json, Company[].class);
而不是
Response response = new Gson().fromJson(json, Response.class);
您可以将异步任务的返回类型更改为List
。这就是我的意思:
public class LoadJSONTask extends AsyncTask<String, Void, List<Company>> {
...
@Override
protected List<Company> doInBackground(String... strings) {
...
List<Company> companies = new Gson().fromJson(stringResponse, new TypeToken<ArrayList<Company>>(){}.getType());
return companies;
...
}
@Override
protected void onPostExecute(List<Company> companies) {
if (companies != null) {
mListener.onLoaded(companies);
} else {
mListener.onError();
}
}
}