AsynHttpClient和SmartImageView控件的使用
来源:互联网 发布:卖客疯之类的软件 编辑:程序博客网 时间:2024/06/07 22:58
我们用AsynHttpClient和SmartImageView控件来做一个新闻发布系统。
首现,我们看一下运行效果图:
中间我们有几点需要注意:、
1、运行前一定要开启Tomcat服务器,在Tomcat服务器根目录下找到bin文件夹,找到 startup.bat,点击运行开启服务器。
2、我们期间,要用到json解析,要在android studio中添加json包,以下是添加json包的过程:
主布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/loading" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:visibility="invisible"> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="正在加载信息..." /> </LinearLayout> <ListView android:id="@+id/lv_news" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout></LinearLayout>
布局中运用到了ListView:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="65dp"> <com.loopj.android.image.SmartImageView android:id="@+id/siv_icon" android:layout_width="80dp" android:layout_height="60dp" android:scaleType="centerCrop" android:src="@mipmap/ic_launcher" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"></com.loopj.android.image.SmartImageView> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginTop="10dp" android:layout_toRightOf="@id/siv_icon" android:ellipsize="end" android:maxLength="20" android:singleLine="true" android:text="我是标题" android:textColor="#000000" android:textSize="18sp" /> <TextView android:id="@+id/tv_description" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_title" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" android:layout_toRightOf="@id/siv_icon" android:ellipsize="end" android:maxLength="16" android:maxLines="1" android:text="我是描述" android:textColor="#99000000" android:textSize="14sp" /> <TextView android:id="@+id/tv_type" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="5dp" android:layout_marginRight="10dp" android:text="评论" android:textColor="#99000000" android:textSize="12sp" /></RelativeLayout>
接下来是json的类:
public class NewsInfo { private String iconPath; private String title; private String description; private int type; private long comment; public String getIconPath() { return iconPath; } public void setIconPath(String iconPath) { this.iconPath = iconPath; } 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; }}
import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;import java.lang.reflect.Type;import java.util.List;import cn.edu.bzu.mynews.entity.NewsInfo;/** * Created by lm on 2017/5/18. */public class JsonParse { public static List<NewsInfo> getNewInfo(String json){ Gson gson=new Gson(); Type listType=new TypeToken<List<NewsInfo>>(){ }.getType(); List<NewsInfo> newsInfos=gson.fromJson(json,listType); return newsInfos; }}
下面是主要的页面交互界面:
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.ArrayList;import java.util.List;import cn.edu.bzu.mynews.Tools.JsonParse;import cn.edu.bzu.mynews.adapter.NewAdapter;import cn.edu.bzu.mynews.entity.NewsInfo;import cn.edu.bzu.mynews.model.Fruit;public class MainActivity extends AppCompatActivity { private ListView Iv_news; private NewAdapter newAdapter; private List<NewsInfo> newInfos; private LinearLayout loading; private JsonParse jsonParse; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Iv_news= (ListView) findViewById(R.id.lv_news); newAdapter =new NewAdapter(this,R.layout.news_item,newInfos); loading= (LinearLayout) findViewById(R.id.loading); prepareData(); } private void prepareData() { AsyncHttpClient asyncHttpClient=new AsyncHttpClient(); asyncHttpClient.get(getString(R.string.serverurl), new AsyncHttpResponseHandler() { @Override public void onSuccess(int i, cz.msebera.android.httpclient.Header[] headers, byte[] bytes) { try { String json=new String(bytes,"utf-8"); newInfos=jsonParse.getNewInfo(json); if(newInfos==null){ Toast.makeText(MainActivity.this,"解析失败",Toast.LENGTH_SHORT).show(); } else { loading.setVisibility(View.INVISIBLE); Iv_news.setAdapter(newAdapter); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } @Override public void onFailure(int i, cz.msebera.android.httpclient.Header[] headers, byte[] bytes, Throwable throwable) { Toast.makeText(MainActivity.this,"请求失败",Toast.LENGTH_SHORT).show(); } }); }}
阅读全文
0 0
- AsynHttpClient和SmartImageView控件的使用
- AsynHttpClient和SmartImageView控件做一个新闻发布系统
- SmartImageView的使用
- SmartImageView的简单使用
- 使用SmartImageView控件实现网络图片的浏览
- 新闻客户端(AsyncHttpClient和SmartImageView的综合使用)
- AsyncHttpClient和SmartImageView的综合使用--新闻客户端
- AsyncHttpClient和SmartImageView的使用及案例(新闻客户端)
- 新闻客户端(AsyncHttpClient和SmartImageView的综合使用
- AsyncHttpClient和SmartImageView的综合使用--新闻客户端
- SmartImageView使用
- Android-----AsyncHttpClient和SmartImageView的概述和使用---案例《新闻客户端》
- Android中综合使用AsyncHttpClient和SmartImageView编写新闻客户端的案例
- Android SmartImageView使用实例
- SmartImageView框架使用
- SmartImageView框架使用
- 【Android之SmartImageView图片控件】
- Android---SmartImageView和AsyncHttpClient的应用之《新闻客户端》案例
- 手把手教你用 TensorFlow 实现文本分类(下)
- 提示网站证书有问题的解决方法
- 【C语言】、模拟实现strlen,strcmp,strcpy,strcat,strstr函数
- Struts2-表单验证的两种方式
- Xcode7报App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insec
- AsynHttpClient和SmartImageView控件的使用
- Linux创建用户后,切换用户报This account is currently not available
- 网络IO之阻塞、非阻塞、同步、异步总结
- location.replace()和location.href=进行跳转的区别
- 玲珑学院1129
- AI眼中的历史:用人工智能挖掘旧报纸里的英国现代史
- 青岛中软卓越java基础教程二:Java的配置和路径选择
- spring boot 热部署
- 基础_C各种表