我尝试从JSON API链接获取数据,如下所示:
{
movies:[
{
id:"8236",
title:"Black Water",
imdb:"tt5622412",
}
]
}
这是主类MainActivity.java:
public class MainActivity extends AppCompatActivity {
private TextView mSearchResultsTextView;
int MovieId = 8236;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test2);
mSearchResultsTextView = (TextView) findViewById(R.id.tv_search_results_json);
makeSearchQuery(String.valueOf(MovieId));
}
private void makeSearchQuery(String sortOrder) {
showJsonDataView();
new test2.QueryTask().execute(sortOrder);
}
private void showJsonDataView() {
mSearchResultsTextView.setVisibility(View.VISIBLE);
}
private void showErrorMessage() {
mSearchResultsTextView.setVisibility(View.INVISIBLE);
}
public class QueryTask extends AsyncTask<String, Void, ArrayList<ShashaItem>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected ArrayList<ShashaItem> doInBackground(String... params) {
if (params.length == 0) {
return null;
}
String searchUrl = params[0];
URL channelRequestUrl = NetworkShasha.buildUrl(searchUrl);
try {
String SearchResults = NetworkShasha.getResponseFromHttpUrl(channelRequestUrl);
ArrayList<ShashaItem> simpleJsonTVData = JsonShashaDetail.getSimpleMovieStringsFromJson(MainActivity.this, SearchResults);
return simpleJsonTVData;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(ArrayList<ShashaItem> SearchResults) {
if (SearchResults != null && !SearchResults.equals("")) {
mSearchResultsTextView.setText(SearchResults.getTitle());
showJsonDataView();
}
else {
showErrorMessage();
}
}
}
}
此行上的getTitle()方法下面有一条红线的问题:
mSearchResultsTextView.setText(SearchResults.getTitle());
onPostExecute中的表示
无法解决此方法
当我调试应用程序时,我发现json响应正确地返回了API的数据,因此每个键都有正确的数据,但是问题在于如何在文本视图中显示标题键的值而不显示错误getTitle()方法
这也是解析器代码JsonShashaDetail.java:
public final class JsonShashaDetail {
public static ArrayList<ShashaItem> getSimpleMovieStringsFromJson(Context context, String moviesJsonString)
throws JSONException {
final String MOVIES = "movies";
final String ID = "id";
final String TITLE = "title";
ArrayList<ShashaItem> parsedMovieData = new ArrayList<ShashaItem>();
JSONObject moviesObject = new JSONObject(moviesJsonString);
JSONArray moviesArray = moviesObject.getJSONArray(MOVIES);
for (int i = 0; i < moviesArray.length(); i++) {
int id;
String title;
moviesObject = moviesArray.getJSONObject(0);
id = moviesObject.getInt(ID);
title = moviesObject.getString(TITLE);
parsedMovieData.add(new ShashaItem(id, title));
}
return parsedMovieData;
}
}
这是getTitle()方法存在的项目的自定义类ShashaItem.java:
public class ShashaItem implements Parcelable {
private int id;
private String title;
public ShashaItem(int id, String title) {
this.id = id;
this.title = title;
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeInt(id);
out.writeString(title);
}
private ShashaItem(Parcel in) {
this.id = in.readInt();
this.title = in.readString();
}
public ShashaItem() {
}
@Override
public int describeContents() {
return 0;
}
public static final Parcelable.Creator<ShashaItem> CREATOR = new Parcelable.Creator<ShashaItem>() {
@Override
public ShashaItem createFromParcel(Parcel in) {
return new ShashaItem(in);
}
@Override
public ShashaItem[] newArray(int i) {
return new ShashaItem[i];
}
};
public int getId() {
return id;
}
public String getTitle() {
return title;
}
}
任何人都可以提供帮助吗?
答案 0 :(得分:3)
在您的MainActivity.java中,我根本看不到githubSearchResults
对象。只有当getTitle()
是[{1}}的实例时才能访问方法githubSearchResults
。
对我来说,解析器本身没有问题。
看起来ShashaItem.class
未实例化,因此它不是githubSearchResults
实例,因此它没有像ShashaItem.class
例如。 getTitle()
你应该有这样的事情:
MainActivity.java
和onPostExecute方法
ShashaItem githubSearchResults;
答案 1 :(得分:2)
您返回的值是List&lt;&gt;一个人没有方法getTitle(),List中的对象是ShashaItem的实例,并且有方法所以你必须得到一个。
此外,您尝试在不存在的对象中调用getTitle,您需要使用从onPostExecute返回的对象:
@Override
protected void onPostExecute(ArrayList<ShashaItem> SearchResults) {
if (SearchResults != null && !SearchResults.equals("")) {
mSearchResultsTextView.setText(SearchResults.get(0).getTitle());