80,349
社区成员
发帖
与我相关
我的任务
分享
//获取《正在热映》的数据
public void moviceTheaterData() {
try {
//获取电影名称
String str = "https://api.douban.com/v2/movie/in_theaters";
URL url = new URL(str);
new MoviceThread(url).start();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
class MoviceThread extends Thread {
URL myurl;
public MoviceThread(URL myurl) {
this.myurl = myurl;
}
@Override
public void run() {
InputStream in = null;
try {
HttpURLConnection conn = (HttpURLConnection) myurl.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
int code = conn.getResponseCode();
if (code == 200) {
in = conn.getInputStream();
BufferedReader reader = null;
reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null)
response.append(line);
json = response + "";
//----------------------------解析数据---------------------------
mHandler.post(new Runnable() {
@Override
public void run() {
jsonMoviceTheaters();
}
});
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//结束子线程,防止占用
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
super.run();
}
}
//解析JSON数据---正在热映
public void jsonMoviceTheaters() {
try {
JSONObject jsonObject = new JSONObject(json);
//第一层解析
JSONArray subjects = jsonObject.optJSONArray("subjects");
for (int i = 0; i < subjects.length(); i++) {
JSONObject jsonObject1 = subjects.getJSONObject(i);
title = jsonObject1.getString("title");
str_id = jsonObject1.getString("id");
id_list_theater.add(str_id);
JSONObject jsonObject2 = jsonObject1.getJSONObject("rating");
double average = jsonObject2.optDouble("average");
JSONObject jsonObject3 = jsonObject1.getJSONObject("images");
String small = jsonObject3.optString("small");
View view1 = mInflater.inflate(R.layout.item_in_theaters,
null, false);
ImageView img = view1.findViewById(R.id.horizon_img);
TextView txt_title = view1.findViewById(R.id.horizon_title);
TextView txt_average = view1.findViewById(R.id.horizon_average);
String straverage = average+"";
txt_title.setText(title);
if(average==0){
txt_average.setText("暂无评分");
}else {
txt_average.setText(straverage);
}
try {
bitmap = ImageService.getImage(small);
} catch (Exception e) {
e.printStackTrace();
}
//图片压缩
img.setImageBitmap(CompressUtils.getInstance(getContext()).compress(bitmap,270,400));
mTheaters.addView(view1);
//点击跳转详情页面
final int pos = i;
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("urlstr",id_list_theater.get(pos));
intent.setClass(getActivity(), MoviceDetailsActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<ImageView
android:id="@+id/horizon_img"
android:layout_width="130dp"
android:layout_height="160dp"
android:src="@drawable/ic_launcher_background" />
<TextView
android:id="@+id/horizon_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/blue"
android:text="疯狂动物城"
android:textSize="18dp" />
<TextView
android:id="@+id/horizon_average"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="9.1"
android:textColor="@color/colorYellow2"
android:textSize="16dp"
android:textStyle="bold" />
</LinearLayout>