Android網(wǎng)絡(luò)編程之簡易新聞客戶端
一、 通過一個案例“新聞客戶端”向大家演示AsyncHttpClient和SmartImageView的綜合使用。
運行結(jié)果如下:

1、首先我們了解一下相關(guān)知識:
SmartImageView的使用
市面上一些常見軟件,例如手機QQ、天貓、京東商場等,都加載了大量網(wǎng)絡(luò)上的圖片。用Android自帶的API實現(xiàn)這一功能十分麻煩而且耗時。為此,編程愛好者開發(fā)了一個開源項目——SmartImageView。
https://github.com/loopj/android-smart-image-view (SmartImageView的jar包得下載)
開源項目SmartImageView的出現(xiàn)主要是為了 加速從網(wǎng)絡(luò)上加載圖片,它繼承自ImageView類,支持根據(jù)URL地址加載圖片、支持異步加載圖片、支持圖片緩存等。
AsyncHttpClient的使用
在Android開發(fā)中,發(fā)送、處理HTTP請求十分常見,如果每次與服務(wù)器進(jìn)行數(shù)據(jù)交互都需要去開啟一個子線程,這樣是非常麻煩的。為了解決這個問題,一些開發(fā)者開發(fā)出了開源項目——AsyncHttpClient。
http://github.com/loopj/android-async-http
http://hc.apache.org/download.cgi
AsyncHttpClient是對HttpClient的 再次包裝。AsyncHttpClient的特點有,發(fā)送 異步HTTP 請求、HTTP
請求發(fā)生在 在UI線程之外 線程之外、內(nèi)部采用了 線程池來處理并發(fā)請求, ,而且它使用起來比HttpClient更加簡便。
配置Tomcat服務(wù)器
http://tomcat.apache.org下載并通過startup.bat啟動服務(wù)器
在webapps/Root文件夾下:JSON文件和images文件夾

在這里我就不介紹GSON解析了,在我的下一篇博文中會有解釋
二、實現(xiàn)步驟如下

需要創(chuàng)建如上類
• Entity包下創(chuàng)建 包下創(chuàng)建實體類 實體類NewsInfo
package cn.edu.bzu.anew.entity;
/**
* Created by Administrator on 2017/5/18.
*/
public class NewsInfo {
private String icon;//圖片路徑
private String title;//新聞標(biāo)題
private String description;//新聞描述
private int type;//新聞類型
private long comment;//新聞評論數(shù)
public NewsInfo(String icon, String title, String description, int type, long comment) {
this.icon = icon;
this.title = title;
this.description = description;
this.type = type;
this.comment = comment;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public long getComment() {
return comment;
}
public void setComment(long comment) {
this.comment = comment;
}
}
• Tools包下創(chuàng)建 包下創(chuàng)建 工具類 類JsonParse 負(fù)責(zé)解析JSON數(shù)據(jù)
package cn.edu.bzu.anew.Tools;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.List;
import cn.edu.bzu.anew.entity.NewsInfo;
/**
* Created by Administrator on 2017/5/18.
*/
public class JsonParse {
public static List<NewsInfo>getNewsInfo(String json){//使用gson庫解析Json數(shù)據(jù)
Gson gson =new Gson();
Type listType=new TypeToken<List<NewsInfo>> (){//創(chuàng)建一個typeToken的匿名子類對象,并調(diào)用對象得getType()方法
}.getType();
List<NewsInfo>newsInfos=gson.fromJson(json,listType);//把獲取到的信息集合存到newsInfos中
return newsInfos;
}
}
adapter 包下創(chuàng)建NewsAdapter類
package cn.edu.bzu.anew.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.loopj.android.image.SmartImageView;
import java.util.List;
import cn.edu.bzu.anew.R;
import cn.edu.bzu.anew.entity.NewsInfo;
public class NewsAdapter extends ArrayAdapter<NewsInfo>{
public NewsAdapter(Context context, List<NewsInfo> objects) {
super(context, R.layout.news_item, objects);
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
NewsInfo newsinfo= getItem(position);//傳遞position,獲取當(dāng)前位置對應(yīng)的newsinfo新聞信息
View view=null;
viewHolder viewHolder;
if(convertView==null){ //判斷convertView中是否加載了布局,有沒有緩存。為空說明沒有緩存
view=LayoutInflater.from(getContext()).inflate(R.layout.news_item,null);
viewHolder=new viewHolder();
viewHolder.siv= (SmartImageView) view.findViewById(R.id.siv_icon);
viewHolder.tv_title= (TextView) view.findViewById(R.id.tv_title);
viewHolder.tv_description= (TextView) view.findViewById(R.id.tv_description);
viewHolder.tv_type= (TextView) view.findViewById(R.id.tv_type);
view.setTag(viewHolder); //保存
}else{
view=convertView;
viewHolder=(viewHolder) view.getTag();
}
viewHolder.siv.setImageUrl(newsinfo.getIcon());//傳遞圖片地址
viewHolder.tv_title.setText(newsinfo.getTitle());//傳遞題目
viewHolder.tv_description.setText(newsinfo.getDescription());
viewHolder.tv_type.setText(newsinfo.getType()+"");
return view;
}
class viewHolder{//添加類,封裝需要查找的控件
TextView tv_title;
TextView tv_description;
TextView tv_type;
SmartImageView siv;
}
}
界面邏輯代碼的設(shè)計與實現(xiàn)(MainActivity)
package cn.edu.bzu.anew;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import java.io.UnsupportedEncodingException;
import java.util.List;
import cn.edu.bzu.anew.Tools.JsonParse;
import cn.edu.bzu.anew.adapter.NewsAdapter;
import cn.edu.bzu.anew.entity.NewsInfo;
public class MainActivity extends AppCompatActivity {
private ListView lvNews;
private List<NewsInfo> newsInfos;
private LinearLayout loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvNews=(ListView)findViewById(R.id.lv_news);
loading=(LinearLayout)findViewById(R.id.loading);
fillData();
}
private void fillData(){
AsyncHttpClient client =new AsyncHttpClient();
client.get("http://10.61.28.176:8080/NewsInfo.json",new AsyncHttpResponseHandler(){
@Override
public void onSuccess(int i, org.apache.http.Header[] headers, byte[] bytes) {
try{
String json=new String(bytes,"utf-8");
newsInfos= JsonParse.getNewsInfo(json);
if(newsInfos==null){
Toast.makeText(MainActivity.this,"解析失敗", Toast.LENGTH_LONG).show();
}else{
loading .setVisibility(View.INVISIBLE);
lvNews.setAdapter(new NewsAdapter(MainActivity.this,newsInfos));
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int i, org.apache.http.Header[] headers, byte[] bytes, Throwable throwable) {
}
} );
}
}
在AndroidManifest.xml添加訪問權(quán)限在</application>外<uses-permission android:name="android.permission.INTERNET"></uses-permission>
最后項目就完成了
有以下注意事項需要我們注意:
(1)我們在自己的電腦上運行項目時要用自己的ip地址 json文件中也是如此
(2)在這里我們需要添加三個jar包,記得as library(在Projects---app---libs)

(3)

如果出現(xiàn)以上問題 ,圖片加載失誤 當(dāng)?shù)刂范颊_ ,那就是你沒有添加網(wǎng)絡(luò)加載圖片還有就是把圖片后綴jpg改為PNG
viewHolder.siv.setImageUrl(newsinfo.getIcon());
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android頁面之間進(jìn)行數(shù)據(jù)回傳的方法分析
這篇文章主要介紹了Android頁面之間進(jìn)行數(shù)據(jù)回傳的方法,結(jié)合實例形式分析了Android頁面之間進(jìn)行數(shù)據(jù)的傳遞與處理技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-06-06
Android根據(jù)電話號碼獲得聯(lián)系人頭像實例代碼
這篇文章主要介紹了Android根據(jù)電話號碼獲得聯(lián)系人頭像實例代碼,是Android程序開發(fā)中非常重要的技巧,需要的朋友可以參考下2014-09-09
Android Studio中導(dǎo)入module的方法(簡單版)
這篇文章主要介紹了AndroidStudio中導(dǎo)入module的方法,本文是一篇簡易版的教程,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-01-01
Android新布局方式ConstraintLayout快速入門教程
谷歌在2016年的IO大會上推出的一種新的布局方式—-ConstraintLayout,這局是一種約束型的布局方式,下面這篇文章主要給大家介紹了Android中新布局方式ConstraintLayout的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-05-05
Android圖片的Base64編碼與解碼及解碼Base64圖片方法
Base64是網(wǎng)絡(luò)上最常見的用于傳輸8Bit字節(jié)碼的編碼方式之一,Base64就是一種基于64個可打印字符來表示二進(jìn)制數(shù)據(jù)的方法。接下來通過本文給大家分享Android圖片的Base64編碼與解碼及解碼Base64圖片,需要的朋友參考下吧2017-12-12

