当我正在编译应用程序时,它不会更新Textviews,即使它没有在Android监视器上显示任何错误。 有人可以解释我如何将JSON映射到java对象和数组吗?
主要Activity.java
public class MainActivity extends AppCompatActivity {
public static final String TAG = MainActivity.class.getSimpleName();
private MovieDetails mmoviedetails;
private TextView Title;
private TextView id;
private TextView releasedate;
private TextView originaltitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Title=(TextView)findViewById(R.id.titleid);
id=(TextView)findViewById(R.id.id);
releasedate=(TextView)findViewById(R.id.releasedateid);
originaltitle=(TextView)findViewById(R.id.originaltitleid);
String forecastUrl = "https://api.themoviedb.org/3/movie/550?api_key=6d349f745d2d8b31d1bfd1cccfbd7e0";
if (isNetworkAvailable()) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(forecastUrl)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
String jsonData = response.body().string();
Log.v(TAG, jsonData);
if (response.isSuccessful()) {
mmoviedetails = getCurrentDetails(jsonData);
runOnUiThread(new Runnable() {
@Override
public void run() {
updateDisplay();
}
});
} else {
alertUserAboutError();
}
}
catch (IOException e) {
Log.e(TAG, "Exception caught: ", e);
}
catch (JSONException e) {
Log.e(TAG, "Exception caught: ", e);
}
}
});
}
else {
Toast.makeText(this, getString(R.string.network_unavailable_message),
Toast.LENGTH_LONG).show();
}
Log.d(TAG, "Main UI code is running!");
}
private void updateDisplay() {
originaltitle.setText(mmoviedetails.getOriginal_title()+"");
id.setText(mmoviedetails.getId()+"");
Title.setText(mmoviedetails.getTitle()+"");
releasedate.setText(mmoviedetails.getRelease_date()+"");
}
private MovieDetails getCurrentDetails(String jsonData) throws JSONException {
JSONObject forecast = new JSONObject(jsonData);
JSONObject currently = forecast.getJSONObject("JSON");
MovieDetails moviedetails = new MovieDetails();
moviedetails.setId(currently.getInt("Id"));
moviedetails.setOriginal_title(currently.getString("Original_title"));
moviedetails.setTitle(currently.getString("Title"));
moviedetails.setRelease_date(currently.getString("Release_date"));
return moviedetails;
}
private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()) {
isAvailable = true;
}
return isAvailable;
}
private void alertUserAboutError() {
}
MovieDetails.java
public class MovieDetails {
private String Title;
private String Release_date;
private int Id;
private String original_title;
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
public String getRelease_date() {
return Release_date;
}
public void setRelease_date(String release_date) {
Release_date = release_date;
}
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getOriginal_title() {
return original_title;
}
public void setOriginal_title(String original_title) {
this.original_title = original_title;
}}
提前致谢。
答案 0 :(得分:0)
你所提到的api没有给出无效的api错误(我假设你故意发错了(y))
api我正在使用我自己的api密钥就是这样的
{
"adult": false,
"backdrop_path": "/wSJPjqp2AZWQ6REaqkMuXsCIs64.jpg",
"belongs_to_collection": null,
"budget": 63000000,
"genres": [
{
"id": 18,
"name": "Drama"
}
],
"homepage": "http://www.foxmovies.com/movies/fight-club",
"id": 550,
"imdb_id": "tt0137523",
"original_language": "en",
"original_title": "Fight Club",
"overview": "A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground \"fight clubs\" forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.",
"popularity": 5.419718,
"poster_path": "/adw6Lq9FiC9zjYEpOqfq03ituwp.jpg",
"production_companies": [
{
"name": "Regency Enterprises",
"id": 508
},
{
"name": "Fox 2000 Pictures",
"id": 711
},
{
"name": "Taurus Film",
"id": 20555
},
{
"name": "Linson Films",
"id": 54050
},
{
"name": "Atman Entertainment",
"id": 54051
},
{
"name": "Knickerbocker Films",
"id": 54052
}
],
"production_countries": [
{
"iso_3166_1": "DE",
"name": "Germany"
},
{
"iso_3166_1": "US",
"name": "United States of America"
}
],
"release_date": "1999-10-14",
"revenue": 100853753,
"runtime": 139,
"spoken_languages": [
{
"iso_639_1": "en",
"name": "English"
}
],
"status": "Released",
"tagline": "How much can you know about yourself if you've never been in a fight?",
"title": "Fight Club",
"video": false,
"vote_average": 8.1,
"vote_count": 5876
}
现在这是一个带有大量jsonarrays的大JSON值
你所制作的POJO,即MovieDetails.java没有上述字段
向你解释一下pojo是如何制作的几点
现在已经到了解你如何解析这些数据的问题 为此你可以使用杰克逊或Gson的图书馆 或者使用JSONObject和JSONArray类
自己做 {}内的元素是JSONObjects,包裹[]的元素是json数组
这里是genre,production_companies,production_countries,spoken_languages是json数组
基本上你的解析就像这样
MainPojo main = new MainPojo();
JSONObject jo = new JSONObject(url);
main.adult = jo.getString("adult"); //remember the string that is passed in as argument should be same as you see in the json and you should be using getter ans setters for assigning values
main.backdrop = jo.getString("backdrop_path"); and so on
.
.
.
.
//now we know genres is a jsonarray so
Genres g;
JSONArray genres = jo.getJSONArray("genres");
for(int i =0;i<genres.length();i++)
{
g = new Genres();
JSONObject genObject = genres.getJSONObject(i);
g.id = genObject.getString("id");
genres.add(g);
}
每隔一个jsonarray都会遵循相同的程序 我建议你从我提到的链接生成api的POJO,以便更好地澄清!!
希望它有所帮助!