您的位置:首页 > 理论基础 > 计算机网络

动态标题+网络异步获取图片与文字

2017-10-11 08:57 316 查看
布局一

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.example.zk.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="你好"
android:textColor="@color/colorAccent"
android:textSize="30dp"
android:id="@+id/tv_news"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv"
android:layout_below="@+id/tv_news"
></ListView>

</RelativeLayout>

布局2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_title"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_date"
/>
</LinearLayout>
//主方法
package com.example.zk;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
private Mydapter mydapter;
private TextView tv;
private ListView lv;
private List<String> datas=new ArrayList<String>();
private List<Beans.NewslistBean> list=new ArrayList<>();
private int index=0;
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
tv.setText(datas.get(index%datas.size()));
index++;
//延迟3秒
sendEmptyMessageDelayed(1,3000);

}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv_news);
lv = (ListView) findViewById(R.id.lv);
mydapter = new Mydapter();

lv.setAdapter(mydapter);
//第二接口的地址
Pathse();
//开启一个线程
new Thread(){
@Override
public void run() {

try {
URL url = new URL("http://www.toutiao.com/hot_words/");
//打开链接,进行请求
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");//请求方式
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
//判断响应码
if (connection.getResponseCode()==200)
{
InputStream inputStream = connection.getInputStream();
String string = strtostring(inputStream);
getJson(string);
handler.sendEmptyMessage(1);
}
} catch (MalformedURLException e) {
e.printStackTrace();
}  catch (IOException e) {
e.printStackTrace();
}
}

}.start();

}
//地址

private void Pathse() {
Asynstring asynstring = new Asynstring();
asynstring.execute("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=1");
}
//适配器
class Mydapter extends BaseAdapter{

@Override
public int getCount() {
return list.size() ;
}

@Override
public Object getItem(int i) {
return list.get(i);
}

@Override
public long getItemId(int i) {
return i;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

ViewHoder vh;
if (view==null){
vh = new ViewHoder();
view= View.inflate(MainActivity.this, R.layout.item, null);
vh.img=(ImageView) view.findViewById(R.id.img);
vh.text=(TextView) view.findViewById(R.id.tv_title);
vh.texts=(TextView) view.findViewById(R.id.tv_date);
view.setTag(vh);
}else {
vh = (ViewHoder) view.getTag();

}
vh.text.setText(list.get(i).getCtime());
vh.texts.setText(list.get(i).getDescription());
Imgs imgs = new Imgs(vh.img);
imgs.execute(list.get(i).getPicUrl());

return view;
}
class  ViewHoder {

public ImageView img;
public TextView text;
public TextView texts;
}
}
//原生态解析
public void getJson(String string) {
try {
JSONArray jsonArray = new JSONArray(string);
for (int i=0;i<jsonArray.length();i++){
String data = jsonArray.optString(i);

datas.add(data);
}
} catch (JSONException e) {
e.printStackTrace();
}

}
public  String strtostring(InputStream inputStream){
StringBuilder stringBuilder = new StringBuilder();

try {
BufferedReader  bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
String con;
while ((con=bufferedReader.readLine())!=null)
{
stringBuilder.append(con);

}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}

class Imgs extends AsyncTask<String,Void,Bitmap>{
private ImageView imageView;

public Imgs(ImageView imageView) {
this.imageView = imageView;
}

@Override
protected Bitmap doInBackground(String... strings) {

try {
URL  url = new URL(strings[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int code = connection.getResponseCode();
if (code==200){
InputStream inputStream = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}

} catch (Exception e) {
e.printStackTrace();
}

return null;
}

@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
imageView.setImageBitmap(bitmap);
}
}
class Asynstring extends AsyncTask<String,Void,String>{

@Override
protected String doInBackground(String... strings) {

try {
URL url = new URL(strings[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int code = connection.getResponseCode();
if (code==200)
{
InputStream inputStream = connection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String string="";
StringBuffer buffer = new StringBuffer();
while ((string=bufferedReader.readLine())!=null){
buffer.append(string);
}
return buffer.toString();

}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
Beans beans = gson.fromJson(s, Beans.class);
List<Beans.NewslistBean> newslist = beans.getNewslist();
list.addAll(newslist);
mydapter.notifyDataSetChanged();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐