适配器:Myadapter
package com.bawi.adapter;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bawi.Bean.Bean;
import com.bawi.httpclient_lx.R;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
public class Myadapter extends BaseAdapter{
private Activity context;
private ArrayList
public static final String IMAGEURL="http://09.imgmini.eastday.com/mobile/20170808/20170808170806_eee032606496949d92efbc906c7140dd_2_mwpm_03200403.jpg";
public Myadapter(Activity context, ArrayList
this.context=context;
this.bean=bean;
}
@Override
public int getCount() {
return bean.size();
}
@Override
public Object getItem(int i) {
return bean.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View converView, ViewGroup viewGroup) {
ViewHolder holder=null;
if(converView==null)
{
converView= LayoutInflater.from(context).inflate(R.layout.item_lv, null);
holder=new ViewHolder();
holder.tv_title= (TextView) converView.findViewById(R.id.tv_title);
holder.tv_category= (TextView) converView.findViewById(R.id.tv_category);
holder.tv_data= https://www.it610.com/article/(TextView) converView.findViewById(R.id.tv_data);
holder.tv_image=(ImageView) converView.findViewById(R.id.tv_image);
converView.setTag(holder);
}else
{
holder= (ViewHolder) converView.getTag();
}
final ViewHolder finalHolder = holder;
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url=new URL(bean.get(i).thumbnail_pic_s);
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
connection.connect();
finalInputStream inputStream=connection.getInputStream();
final Bitmap bitmap= BitmapFactory.decodeStream(inputStream);
context.runOnUiThread(new Runnable() {
@Override
public void run() {
finalHolder.tv_image.setImageBitmap(bitmap);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
holder.tv_title.setText(bean.get(i).title);
holder.tv_category.setText(bean.get(i).category);
holder.tv_data.setText(bean.get(i).date);
return converView;
}
public class ViewHolder
{
public TextView tv_title,tv_category,tv_data;
public ImageView tv_image;
}
}
Bean类
package com.bawi.Bean;
public class Bean {
public String url;
public String title;
public String date;
public String category;
public String author_name;
public String thumbnail_pic_s;
}
主界面:MainActivity
package com.bawi.httpclient_lx;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import com.bawi.Bean.Bean;
import com.bawi.adapter.Myadapter;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ImageView tv_image;
private String responsemsg;
private Myadapter ma;
private ListView lv;
private ArrayList
private final int SUCCESS=0;
private final int FALT=-1;
private final String GETURL="http://v.juhe.cn/toutiao/index?type=top&&key=22a108244dbb8d1f49967cd74a0c144d";
Handler myhandler=new Handler() {
public void handleMessage(Message msg){
int what = msg.what;
switch (what){
case SUCCESS:
String result = (String) msg.obj;
Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show();
pareseDATA(result);
break;
case FALT:
Toast.makeText(MainActivity.this,responsemsg,Toast.LENGTH_SHORT).show();
break;
}
}
};
//JSON解析
private void pareseDATA(String result) {
try {
JSONObject obj=new JSONObject(result);
JSONObject resultobj = obj.getJSONObject("result");
JSONArray dataArray = resultobj.getJSONArray("data");
if(dataArray.length()>0&&dataArray!=null)
{
for (int i = 0;
i
Bean bean=new Bean();
bean.title= data.getString("title");
bean.date=data.getString("date");
bean.thumbnail_pic_s=data.getString("thumbnail_pic_s");
bean.category=data.getString("category");
bean.url=data.optString("url");
list.add(bean);
}
System.out.println(list);
flushnum();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
//创建线程
new Thread(new Runnable() {
@Override
public void run() {
getNews();
}
}).start();
}
private void getNews() {
StringBuffer result=new StringBuffer();
//创建HttpClient对象,打开一个浏览器
HttpClient httpclient = new DefaultHttpClient();
//创建HttpGet对象,get请求对象,传入url
HttpGet httpget=new HttpGet(GETURL);
try {
//发送请求,返回Httprespones对象,这是服务器返回的对象
HttpResponse httpResponse = httpclient.execute(httpget);
//根据相应行,拿到响应码,getStatusLine:相应行;; getStatusCode:响应码
int responseCode = httpResponse.getStatusLine().getStatusCode();
responsemsg = httpResponse.getStatusLine().getReasonPhrase();
//判断响应码是否为200,如果是则请求成功否则失败
if(200==responseCode)
{
//得到返回的实体对象
HttpEntity entity = httpResponse.getEntity();
InputStream inputStream = entity.getContent();
BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
String line=null;
while((line=reader.readLine())!=null)
{
result.append(line);
}
Message msg=new Message();
msg.what=SUCCESS;
msg.obj=result.toString();
myhandler.sendMessage(msg);
}else
{
Message msg=new Message();
msg.what=FALT;
myhandler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
httpclient.getConnectionManager().shutdown();
}
}
private void initData() {
list=new ArrayList
ma=new Myadapter(this,list);
}
private void flushnum()
{
ma=new Myadapter(this,list);
lv.setAdapter(ma);
lv.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
Bean bean = (Bean) ma.getItem(i);
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
intent.putExtra("url",bean.url);
intent.putExtra("title",bean.title);
startActivity(intent);
}
});
}
//初始化数据
private void initView() {
lv= (ListView) findViewById(R.id.lv);
}
}
【adapter|HttpClient网络请求获取网络图片】
webView获取
package com.bawi.httpclient_lx;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.WebView;
public class OtherActivity extends AppCompatActivity {
private WebView wv;
private String title;
private String url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
initView();
initData();
LoadWebView();
}
private void initData() {
if(getIntent().getExtras()!=null)
{
title = getIntent().getExtras().getString("title");
url=getIntent().getExtras().getString("url");
if(!TextUtils.isEmpty(title))
{
setTitle(title);
}else
{
setTitle("详情");
}
}
}
private void LoadWebView() {
wv.loadUrl(url);
}
private void initView() {
wv= (WebView) findViewById(R.id.wv);
}
}
主布局
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_width="match_parent"
android:layout_height="match_parent">
webView布局
文章图片
文章图片
文章图片
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="match_parent">
listView布局
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_width="80dip"
android:layout_height="80dip"
/>
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:textSize="24dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:orientation="horizontal"
android:weightSum="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:layout_weight="1"
android:layout_marginTop="15dp"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_weight="1"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
推荐阅读
- 工具|Spring特点中关于DI,IOC及AOP的个人理解
- main|Okhttp网络请求
- xml|完整购物车
- adapter|okHttp网络请求2——MVP
- javaweb|用quartz开发调度任无法注入bean,出现空指针问题
- CodeSeg|Spring,Struts整合时Action中如何获得Bean工厂
- autowired|@Autowired 注释遇到的问题,@Qualifier 帮助解决问题