package com.example.administrator.a01_zhoukao;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private List<DataDataBean.NewslistBean> list = new ArrayList<>();
private MyAdapter myAdapter;
private List<DataDataBean.NewslistBean> list = new ArrayList<>();
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_view);
}
public void httpClient(View view){
list.clear();
setAdapter();
list.clear();
setAdapter();
MyTask myTask = new MyTask();
myTask.execute("httpClient");
}
myTask.execute("httpClient");
}
public void httpConnection(View view){
list.clear();
setAdapter();
list.clear();
setAdapter();
MyTask myTask = new MyTask();
myTask.execute("httpConnection");
}
myTask.execute("httpConnection");
}
class MyTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
protected String doInBackground(String... strings) {
if ("httpClient".equals(strings[0])){
try {
//...........加一行代码...证书不匹配的主机名 的问题...org.apache.http有自己的一套SSL东西,修改org.apache.http的主机名验证解决问题
SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier());
//1.创建一个httpClient客户端对象...HttpClient是一个接口..DefaultHttpClient实现类
HttpClient client = new DefaultHttpClient();
//路径不再需要转为url,直接使用字符串即可
String path = "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10";
//2.创建一个httpGet请求对象
HttpGet httpGet = new HttpGet(path);
//3.使用客户端对象执行一个get请求...返回值是一个响应的对象
HttpResponse response = client.execute(httpGet);
try {
//...........加一行代码...证书不匹配的主机名 的问题...org.apache.http有自己的一套SSL东西,修改org.apache.http的主机名验证解决问题
SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier());
//1.创建一个httpClient客户端对象...HttpClient是一个接口..DefaultHttpClient实现类
HttpClient client = new DefaultHttpClient();
//路径不再需要转为url,直接使用字符串即可
String path = "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10";
//2.创建一个httpGet请求对象
HttpGet httpGet = new HttpGet(path);
//3.使用客户端对象执行一个get请求...返回值是一个响应的对象
HttpResponse response = client.execute(httpGet);
//4.获取响应码....先拿到响应状态行,,,在拿到响应的状态码
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200){
//5.以流的形式获取服务器写回的内容...先拿到响应的实体内容对象,,,再拿响应的实体内容
InputStream inputStream = response.getEntity().getContent();
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200){
//5.以流的形式获取服务器写回的内容...先拿到响应的实体内容对象,,,再拿响应的实体内容
InputStream inputStream = response.getEntity().getContent();
String json = streamToString(inputStream,"utf-8");
return json;
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}else if ("httpConnection".equals(strings[0])){
String path = "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10";
String path = "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10";
try {
URL url = new URL(path);
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
//获取
int responseCode = connection.getResponseCode();
if (responseCode == 200){
//返回的字节流
InputStream inputStream = connection.getInputStream();
int responseCode = connection.getResponseCode();
if (responseCode == 200){
//返回的字节流
InputStream inputStream = connection.getInputStream();
//把字节流转成字符串
String json = streamToString(inputStream,"utf-8");
return json;
}
String json = streamToString(inputStream,"utf-8");
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
}
e.printStackTrace();
}
}
return null;
}
}
@Override
protected void onPostExecute(String s) {
Gson gson = new Gson();
protected void onPostExecute(String s) {
Gson gson = new Gson();
DataDataBean dataDataBean = gson.fromJson(s, DataDataBean.class);
if (dataDataBean != null){
list.addAll(dataDataBean.getNewslist());
setAdapter();
}
}
}
}
private void setAdapter(){
if (myAdapter == null){
myAdapter = new MyAdapter(MainActivity.this, list);
listView.setAdapter(myAdapter);
}else {
myAdapter.notifyDataSetChanged();
}
if (myAdapter == null){
myAdapter = new MyAdapter(MainActivity.this, list);
listView.setAdapter(myAdapter);
}else {
myAdapter.notifyDataSetChanged();
}
}
private String streamToString(InputStream inputStream,String charset) {
//转成字符流
try {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,charset);
try {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,charset);
//转成缓冲流
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
//读取
String s = null;
StringBuilder builder = new StringBuilder();
String s = null;
StringBuilder builder = new StringBuilder();
while ((s = bufferedReader.readLine()) != null){
builder.append(s);
}
builder.append(s);
}
bufferedReader.close();
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
e.printStackTrace();
}
return null;
}
}
}
---------------------------------------------------------
package com.example.administrator.a01_zhoukao;
import java.util.List;
/**
* @author Dash
* @date 2017/9/4
* @description:
*/
public class DataDataBean {
* @author Dash
* @date 2017/9/4
* @description:
*/
public class DataDataBean {
/**
* code : 200
* msg : success
* newslist : [{"ctime":"2017-09-04","description":"Kindle杂志公社","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-41640356.jpg/640","title":"?董卿都还在跪着采访,你却在自习室吃酱香饼","url":"https://mp.weixin.qq.com/s?src=16&ver=346×tamp=1504497622&signature=-wC3QN6HvGwbYybTaUakDD-eDfuMAbch4Sb9l9pHja*ecm295sXl7C0L9LK50p-7WBQYQJIbMkmUdS4-QU6UFJ*fWZKkX6i3CYSm8iX-X6M="},{"ctime":"2017-09-04","description":"肌肉男训练营","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-41959345.static/640","title":"高级私教给你的15条减肥忠告,不看就亏大了!","url":"https://mp.weixin.qq.com/s?src=16&ver=346×tamp=1504497622&signature=hKR*xlidtwQjVlLnkZEMbUIb6yvtjVP4Ao9WJY5YE7lSOzcAIAk4mn6Y9-p*njiWOTS8P4YGIqUiB6r7rd*Y2CDFSg-q0IgVCZ1N7UZpECs="},{"ctime":"2017-09-04","description":"圣经百科","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-40571375.static/640","title":"新的一周,你别太累了\u2026一首《不要活的太累》送给每一位弟兄姊妹!","url":"https://mp.weixin.qq.com/s?src=16&ver=346×tamp=1504497622&signature=5lI2fpBsw70KOGYomOWTYuNfxoDSPilNvIbJS7FXQtevJKiazJFMa8UCG-q0h-B7fP6otk4PJH6uONkKLF2Vxo4fJ*NH0pEF*S1qe4HZTDE="},{"ctime":"2017-09-04","description":"飞行员的那些事","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-37082970.jpg/640","title":"离开了位子你是谁?","url":"https://mp.weixin.qq.com/s?src=16&ver=346×tamp=1504497621&signature=1KOe-MM-uJH0niS1hLWYCDZ8pl*n4q5XMKdPamXYxfAcDMvnsgI0vyvYIVOmKZLDFjqelydO3jW3BBCFfc9LYIxvT-q0eLNarEz5bcOwVsg="},{"ctime":"2017-09-04","description":"品茶时刻","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-38910532.jpg/640","title":"此文无价,很短很深","url":"https://mp.weixin.qq.com/s?src=16&ver=346×tamp=1504497621&signature=CZ93LEr4bDwAiG6RvQJGUnxCkAfoEmj01dmWpugarWSYoHhbrSwQjWbnILtnEbIdonbPSIGPTEz95CD1Nlp4F5ZvCCloBwyxP7MrGGQsDi0="},{"ctime":"2017-09-04","description":"农合论谈","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-38384755.jpg/640","title":"叫醒自己的24张图","url":"https://mp.weixin.qq.com/s?src=16&ver=346×tamp=1504497621&signature=qhq3egj0Ay85fPuv1*qf3Jh*oatEWEVc5nrKyOU8qTD4gFacrcX7zz*rWvt0x1mwitsM6W4x4SKCo4laZofpgIxgarSpm2qGLx4RGQ3oRIk="},{"ctime":"2017-09-04","description":"解夢","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-33132301.jpg/640","title":"少和这种人在一起,且行且珍惜!","url":"https://mp.weixin.qq.com/s?src=16&ver=346×tamp=1504497620&signature=Ip9eY0dMmP6TLtE8rvQ7RKPwtRHEefkesRC19Y0dlUuTBlBpZhmAkmwRYZc5qqGoOmJfgndVQGQD5bD3HxDoXfFsk24wuvXK-4zecKExSoI="},{"ctime":"2017-09-04","description":"宁朝晖","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-36695334.jpg/640","title":"家庭和睦,再穷都能发家(太准了)","url":"https://mp.weixin.qq.com/s?src=16&ver=346×tamp=1504497620&signature=obe47fN3HGHRct-ac5j-g8elhikth2k5S3N8qFmqkG*wDClVenprgUudhuY44XIA-rXDpLigkngj5kEa18w3Qj6Bj5JU4NZKgEKhglC6Z9Q="},{"ctime":"2017-09-04","description":"十二星座运势","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-34472213.static/640","title":"珍惜身边麻烦你的人","url":"https://mp.weixin.qq.com/s?src=16&ver=346×tamp=1504497620&signature=Hy8herdFdb4JH8AVO9nBDxMURmf7HAvCTU9DzjcReplG*LxJSvjd4Rv28AevArPKwrCD*ikL81DUBECdJStS0K3bUgUjHfKXy-tvjvhb74k="},{"ctime":"2017-09-04","description":"文刀米","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-41981707.jpg/640","title":"用业余时间画时装画的春然,不仅出了一本麦昆作品集","url":"https://mp.weixin.qq.com/s?src=16&ver=346×tamp=1504497619&signature=q-cuQjb-G-s-NZXUthLB3Yij917Gizv9vqnRNQgW5IXoR1zJEuy4P9mh0*cqR4pS5jrrq-POjgalt3J1o8bP2sTk2rKfX*AyxJ6NZ16NqNY="}]
*/
* code : 200
* msg : success
* newslist : [{"ctime":"2017-09-04","description":"Kindle杂志公社","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-41640356.jpg/640","title":"?董卿都还在跪着采访,你却在自习室吃酱香饼","url":"https://mp.weixin.qq.com/s?src=16&ver=346×tamp=1504497622&signature=-wC3QN6HvGwbYybTaUakDD-eDfuMAbch4Sb9l9pHja*ecm295sXl7C0L9LK50p-7WBQYQJIbMkmUdS4-QU6UFJ*fWZKkX6i3CYSm8iX-X6M="},{"ctime":"2017-09-04","description":"肌肉男训练营","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-41959345.static/640","title":"高级私教给你的15条减肥忠告,不看就亏大了!","url":"https://mp.weixin.qq.com/s?src=16&ver=346×tamp=1504497622&signature=hKR*xlidtwQjVlLnkZEMbUIb6yvtjVP4Ao9WJY5YE7lSOzcAIAk4mn6Y9-p*njiWOTS8P4YGIqUiB6r7rd*Y2CDFSg-q0IgVCZ1N7UZpECs="},{"ctime":"2017-09-04","description":"圣经百科","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-40571375.static/640","title":"新的一周,你别太累了\u2026一首《不要活的太累》送给每一位弟兄姊妹!","url":"https://mp.weixin.qq.com/s?src=16&ver=346×tamp=1504497622&signature=5lI2fpBsw70KOGYomOWTYuNfxoDSPilNvIbJS7FXQtevJKiazJFMa8UCG-q0h-B7fP6otk4PJH6uONkKLF2Vxo4fJ*NH0pEF*S1qe4HZTDE="},{"ctime":"2017-09-04","description":"飞行员的那些事","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-37082970.jpg/640","title":"离开了位子你是谁?","url":"https://mp.weixin.qq.com/s?src=16&ver=346×tamp=1504497621&signature=1KOe-MM-uJH0niS1hLWYCDZ8pl*n4q5XMKdPamXYxfAcDMvnsgI0vyvYIVOmKZLDFjqelydO3jW3BBCFfc9LYIxvT-q0eLNarEz5bcOwVsg="},{"ctime":"2017-09-04","description":"品茶时刻","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-38910532.jpg/640","title":"此文无价,很短很深","url":"https://mp.weixin.qq.com/s?src=16&ver=346×tamp=1504497621&signature=CZ93LEr4bDwAiG6RvQJGUnxCkAfoEmj01dmWpugarWSYoHhbrSwQjWbnILtnEbIdonbPSIGPTEz95CD1Nlp4F5ZvCCloBwyxP7MrGGQsDi0="},{"ctime":"2017-09-04","description":"农合论谈","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-38384755.jpg/640","title":"叫醒自己的24张图","url":"https://mp.weixin.qq.com/s?src=16&ver=346×tamp=1504497621&signature=qhq3egj0Ay85fPuv1*qf3Jh*oatEWEVc5nrKyOU8qTD4gFacrcX7zz*rWvt0x1mwitsM6W4x4SKCo4laZofpgIxgarSpm2qGLx4RGQ3oRIk="},{"ctime":"2017-09-04","description":"解夢","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-33132301.jpg/640","title":"少和这种人在一起,且行且珍惜!","url":"https://mp.weixin.qq.com/s?src=16&ver=346×tamp=1504497620&signature=Ip9eY0dMmP6TLtE8rvQ7RKPwtRHEefkesRC19Y0dlUuTBlBpZhmAkmwRYZc5qqGoOmJfgndVQGQD5bD3HxDoXfFsk24wuvXK-4zecKExSoI="},{"ctime":"2017-09-04","description":"宁朝晖","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-36695334.jpg/640","title":"家庭和睦,再穷都能发家(太准了)","url":"https://mp.weixin.qq.com/s?src=16&ver=346×tamp=1504497620&signature=obe47fN3HGHRct-ac5j-g8elhikth2k5S3N8qFmqkG*wDClVenprgUudhuY44XIA-rXDpLigkngj5kEa18w3Qj6Bj5JU4NZKgEKhglC6Z9Q="},{"ctime":"2017-09-04","description":"十二星座运势","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-34472213.static/640","title":"珍惜身边麻烦你的人","url":"https://mp.weixin.qq.com/s?src=16&ver=346×tamp=1504497620&signature=Hy8herdFdb4JH8AVO9nBDxMURmf7HAvCTU9DzjcReplG*LxJSvjd4Rv28AevArPKwrCD*ikL81DUBECdJStS0K3bUgUjHfKXy-tvjvhb74k="},{"ctime":"2017-09-04","description":"文刀米","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-41981707.jpg/640","title":"用业余时间画时装画的春然,不仅出了一本麦昆作品集","url":"https://mp.weixin.qq.com/s?src=16&ver=346×tamp=1504497619&signature=q-cuQjb-G-s-NZXUthLB3Yij917Gizv9vqnRNQgW5IXoR1zJEuy4P9mh0*cqR4pS5jrrq-POjgalt3J1o8bP2sTk2rKfX*AyxJ6NZ16NqNY="}]
*/
private int code;
private String msg;
private List<NewslistBean> newslist;
private String msg;
private List<NewslistBean> newslist;
public int getCode() {
return code;
}
return code;
}
public void setCode(int code) {
this.code = code;
}
this.code = code;
}
public String getMsg() {
return msg;
}
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
this.msg = msg;
}
public List<NewslistBean> getNewslist() {
return newslist;
}
return newslist;
}
public void setNewslist(List<NewslistBean> newslist) {
this.newslist = newslist;
}
this.newslist = newslist;
}
public static class NewslistBean {
/**
* ctime : 2017-09-04
* description : Kindle杂志公社
* picUrl : https://zxpic.gtimg.com/infonew/0/wechat_pics_-41640356.jpg/640
* title : ?董卿都还在跪着采访,你却在自习室吃酱香饼
* url : https://mp.weixin.qq.com/s?src=16&ver=346×tamp=1504497622&signature=-wC3QN6HvGwbYybTaUakDD-eDfuMAbch4Sb9l9pHja*ecm295sXl7C0L9LK50p-7WBQYQJIbMkmUdS4-QU6UFJ*fWZKkX6i3CYSm8iX-X6M=
*/
/**
* ctime : 2017-09-04
* description : Kindle杂志公社
* picUrl : https://zxpic.gtimg.com/infonew/0/wechat_pics_-41640356.jpg/640
* title : ?董卿都还在跪着采访,你却在自习室吃酱香饼
* url : https://mp.weixin.qq.com/s?src=16&ver=346×tamp=1504497622&signature=-wC3QN6HvGwbYybTaUakDD-eDfuMAbch4Sb9l9pHja*ecm295sXl7C0L9LK50p-7WBQYQJIbMkmUdS4-QU6UFJ*fWZKkX6i3CYSm8iX-X6M=
*/
private String ctime;
private String description;
private String picUrl;
private String title;
private String url;
private String description;
private String picUrl;
private String title;
private String url;
public String getCtime() {
return ctime;
}
return ctime;
}
public void setCtime(String ctime) {
this.ctime = ctime;
}
this.ctime = ctime;
}
public String getDescription() {
return description;
}
return description;
}
public void setDescription(String description) {
this.description = description;
}
this.description = description;
}
public String getPicUrl() {
return picUrl;
}
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
this.picUrl = picUrl;
}
public String getTitle() {
return title;
}
return title;
}
public void setTitle(String title) {
this.title = title;
}
this.title = title;
}
public String getUrl() {
return url;
}
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
}
this.url = url;
}
}
}
-------------------------------------------------------------
package com.example.administrator.a01_zhoukao;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* @author Dash
* @date 2017/9/4
* @description:
*/
public class MyAdapter extends BaseAdapter {
Context context;
List<DataDataBean.NewslistBean> list;
* @author Dash
* @date 2017/9/4
* @description:
*/
public class MyAdapter extends BaseAdapter {
Context context;
List<DataDataBean.NewslistBean> list;
public MyAdapter(Context context, List<DataDataBean.NewslistBean> list) {
this.context = context;
this.list = list;
}
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view == null){
view = View.inflate(context,R.layout.item_layout,null);
holder = new ViewHolder();
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view == null){
view = View.inflate(context,R.layout.item_layout,null);
holder = new ViewHolder();
holder.textView = view.findViewById(R.id.item_text);
holder.imageView = view.findViewById(R.id.item_image);
holder.imageView = view.findViewById(R.id.item_image);
view.setTag(holder);
}else {
holder = (ViewHolder) view.getTag();
}
}else {
holder = (ViewHolder) view.getTag();
}
holder.textView.setText(list.get(i).getTitle());
return view;
}
}
class ViewHolder{
TextView textView;
ImageView imageView;
}
}
------------------------------
TextView textView;
ImageView imageView;
}
}