栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

android 新浪微博客户端的表情功能的实现,flutter教程pdf

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

android 新浪微博客户端的表情功能的实现,flutter教程pdf

4.实现一个可以让用户选择的表情界面,本人用GridView实现

5.实现点击GridView的每一个item,处理根据item的index查找对应的表情code,然后再把code利用正则把code转换为相对应的表情图片,最后表情插入EditText进行发送。

下面是具体的实现过程

1.把新浪表情图片下载到本地的实现如下:(这个可以建一个java工程进行下载)

Java代码  

    public void getFriendList() throws Exception {

    BlogReleaseServiceImpl service = new BlogReleaseServiceImpl();

    List list = service.getEmotion();

    for (Emotions emotions : list) {

    String path = emotions.getUrl();

    String filename = path.substring(path.lastIndexOf("/") + 1,path.length());

    URL url =  new URL(path);

    HttpURLConnection conn = (HttpURLConnection)url.openConnection();

    conn.setRequestMethod(“GET”);

    conn.setReadTimeout(5 * 1000);

    if(conn.getResponseCode() == 200){

    InputStream is = conn.getInputStream();

    byte[] data = readStream(is);

    File file = new File(“f: \sina_images\” + filename);

    FileOutputStream fs = new FileOutputStream(file);

    fs.write(data);

    fs.close();

    }else{

    System.out.println(“请求失败”);

    }

    }

    }

    public byte[] readStream(InputStream is) throws Exception {

    ByteArrayOutputStream os = new ByteArrayOutputStream();

    byte[] buffer = new byte[2048];

    int len = 0;

    while((len = is.read(buffer)) != -1){

    os.write(buffer,0,len);

    }

    is.close();

    return os.toByteArray();

    }

public void getFriendList() throws Exception {

BlogReleaseServiceImpl service = new BlogReleaseServiceImpl();

List list = service.getEmotion();

for (Emotions emotions : list) {

String path = emotions.getUrl();

String filename = path.substring(path.lastIndexOf("/") + 1,path.length());

URL url = new URL(path);

HttpURLConnection conn = (HttpURLConnection)url.openConnection();

conn.setRequestMethod(“GET”);

conn.setReadTimeout(5 * 1000);

if(conn.getResponseCode() == 200){

InputStream is = conn.getInputStream();

byte[] data = readStream(is);

File file = new File(“f: sina_images” + filename);

FileOutputStream fs = new FileOutputStream(file);

fs.write(data);

fs.close();

}else{

System.out.println(“请求失败”);

}

}

}

public byte[] readStream(InputStream is) throws Exception {

ByteArrayOutputStream os = new ByteArrayOutputStream();

byte[] buffer = new byte[2048];

int len = 0;

while((len = is.read(buffer)) != -1){

os.write(buffer,0,len);

}

is.close();

return os.toByteArray();

}

2:把本地的表情都放进android的资源文件里----drawable下面(这个就不用多说了,直接选取所有文件复制就行了)

3:

3.1访问新浪的表情接口,把返回的信息如下:

Xml代码  

    [嘻嘻]

    face

    http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/c2/tooth.gif

    false

    true

    0

    lt;/emotion>

[嘻嘻]

face

http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/c2/tooth.gif

false

true

0

3.2 储存在一个Emotion.java里。Emotion.java代码如下:

Java代码  

    package com.uim.microblog.model;

    import java.io.Serializable;

    public class Emotions implements Serializable {

    private static final long serialVersionUID = 1L;

    private String phrase;//表情使用的替代文字

    private String type;

    private String url;//表情图片存放的位置

    private String isHot;//是否为热门表情

    private String isCommon;//是否属于通用

    private String orderNumber;//该表情在系统中的排序号码

    private String category;//表情分类

    private String imageName;//表情名称

    public String getImageName() {

    return imageName;

    }

    public void setImageName(String imageName) {

    this.imageName = imageName;

    }

    public String getPhrase() {

    return phrase;

    }

    public void setPhrase(String phrase) {

    this.phrase = phrase;

    }

    public String getType() {

    return type;

    }

    public void setType(String type) {

    this.type = type;

    }

    public String getUrl() {

    return url;

    }

    public void setUrl(String url) {

    this.url = url;

    }

    public String getIsHot() {

    return isHot;

    }

    public void setIsHot(String isHot) {

    this.isHot = isHot;

    }

    public String getIsCommon() {

    return isCommon;

    }

    public void setIsCommon(String isCommon) {

    this.isCommon = isCommon;

    }

    public String getOrderNumber() {

    return orderNumber;

    }

    public void setOrderNumber(String orderNumber) {

    this.orderNumber = orderNumber;

    }

    public String getCategory() {

    return category;

    }

    public void setCategory(String category) {

    this.category = category;

    }

    }

package com.uim.microblog.model;

import java.io.Serializable;

public class Emotions implements Serializable {

private static final long serialVersionUID = 1L;

private String phrase;//表情使用的替代文字

private String type;

private String url;//表情图片存放的位置

private String isHot;//是否为热门表情

private String isCommon;//是否属于通用

private String orderNumber;//该表情在系统中的排序号码

private String category;//表情分类

private String imageName;//表情名称

public String getImageName() {

return imageName;

}

public void setImageName(String imageName) {

this.imageName = imageName;

}

public String getPhrase() {

return phrase;

}

public void setPhrase(String phrase) {

this.phrase = phrase;

}

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

public String getUrl() {

return url;

}

public void setUrl(String url) {

this.url = url;

}

public String getIsHot() {

return isHot;

}

public void setIsHot(String isHot) {

this.isHot = isHot;

}

public String getIsCommon() {

return isCommon;

}

public void setIsCommon(String isCommon) {

this.isCommon = isCommon;

}

public String getOrderNumber() {

return orderNumber;

}

public void setOrderNumber(String orderNumber) {

this.orderNumber = orderNumber;

}

public String getCategory() {

return category;

}

public void setCategory(String category) {

this.category = category;

}

}

3.3xm sax解析l的handler如下:

Java代码  

    package com.uim.microblog.net.handler;

    import java.util.ArrayList;

    import java.util.List;

    import org.xml.sax.Attributes;

    import org.xml.sax.SAXException;

    import org.xml.sax.helpers.DefaultHandler;

    import com.uim.microblog.model.Emotions;

    import com.uim.microblog.model.ResponseResult;

    public class BlogEmotionsHandler extends DefaultHandler {

    private List list;

    private Emotions emotions;

    private ResponseResult responseresult;

    private String tag = null;//正在解析的元素

    public List getEmotionsList(){

    return list;

    }

    @Override

    public void characters(char[] ch, int start, int length)

    throws SAXException {

    if (tag != null) {

    String textarea = new String(ch,start,length);

    if (“phrase”.equals(tag)) {

    emotions.setPhrase(textarea);

    } else if (“type”.equals(tag)) {

    emotions.setType(textarea);

    } else if (“url”.equals(tag)) {

    try {

    emotions.setUrl(textarea);

    String imageName = textarea.substring(textarea.lastIndexOf("/") + 1,textarea.length() - 4);

    emotions.setImageName(imageName);

    } catch (Exception e) {

    e.printStackTrace();

    }

    } else if (“is_hot”.equals(tag)) {

    emotions.setIsHot(textarea);

    } else if (“is_common”.equals(tag)) {

    emotions.setIsCommon(textarea);

    } else if (“order_number”.equals(tag)) {

    emotions.setOrderNumber(textarea);

    } else if (“category”.equals(tag)) {

    emotions.setCategory(textarea);

    } else if (“retn”.equals(tag)) {

    responseresult.setRetn(textarea);

    } else if (“desc”.equals(tag)) {

    responseresult.setDesc(textarea);

    }

    }

    }

    @Override

    public void enddocument() throws SAXException {

    super.enddocument();

    }

    @Override

    public void endElement(String uri, String localName, String qName)

    throws SAXException {

    tag = null;

    if (“mb”.equals(localName)) {

    } else if (“emotions”.equals(localName)) {

    responseresult =null;

    } else if (“emotion”.equals(localName)) {

    list.add(emotions);

    emotions = null;

    }

    }

    @Override

    public void startdocument() throws SAXException {

    list = new ArrayList();

    }

    @Override

    public void startElement(String uri, String localName, String qName,

    Attributes attributes) throws SAXException {

    if (“mb”.equals(localName)) {

    responseresult = new ResponseResult();

    } else if (“emotions”.equals(localName)) {

    } else if (“emotion”.equals(localName)) {

    emotions = new Emotions();

    }

    tag = localName;

    }

    }

package com.uim.microblog.net.handler;

import java.util.ArrayList;

import java.util.List;

import org.xml.sax.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

import com.uim.microblog.model.Emotions;

import com.uim.microblog.model.ResponseResult;

public class BlogEmotionsHandler extends DefaultHandler {

private List list;

private Emotions emotions;

private ResponseResult responseresult;

private String tag = null;//正在解析的元素

public List getEmotionsList(){

return list;

}

@Override

public void characters(char[] ch, int start, int length)

throws SAXException {

if (tag != null) {

String textarea = new String(ch,start,length);

/*开始解析表情数据/

if (“phrase”.equals(tag)) {

emotions.setPhrase(textarea);

} else if (“type”.equals(tag)) {

emotions.setType(textarea);

} else if (“url”.equals(tag)) {

try {

emotions.setUrl(textarea);

String imageName = textarea.substring(textarea.lastIndexOf("/") + 1,textarea.length() - 4);

emotions.setImageName(imageName);

} catch (Exception e) {

e.printStackTrace();

}

} else if (“is_hot”.equals(tag)) {

emotions.setIsHot(textarea);

} else if (“is_common”.equals(tag)) {

emotions.setIsCommon(textarea);

} else if (“order_number”.equals(tag)) {

emotions.setOrderNumber(textarea);

} else if (“category”.equals(tag)) {

emotions.setCategory(textarea);

} else if (“retn”.equals(tag)) {

responseresult.setRetn(textarea);

} else if (“desc”
.equals(tag)) {

responseresult.setDesc(textarea);

}

}

}

@Override

public void enddocument() throws SAXException {

super.enddocument();

}

@Override

public void endElement(String uri, String localName, String qName)

throws SAXException {

tag = null;

if (“mb”.equals(localName)) {

} else if (“emotions”.equals(localName)) {

responseresult =null;

} else if (“emotion”.equals(localName)) {

list.add(emotions);

emotions = null;

}

}

@Override

public void startdocument() throws SAXException {

list = new ArrayList();

}

@Override

public void startElement(String uri, String localName, String qName,

Attributes attributes) throws SAXException {

if (“mb”.equals(localName)) {

responseresult = new ResponseResult();

} else if (“emotions”.equals(localName)) {

} else if (“emotion”.equals(localName)) {

emotions = new Emotions();

}

tag = localName;

}

}

3.4sax解析

Java代码  

    public List getEmotion(){

    BlogGetData getdata = new BlogGetData();

    String result = getdata.blogEmotionsServlet();

    try {

    //生成SAX解析对象

    parser = SAXParserFactory.newInstance().newSAXParser();

    //生成xml读取器

    reader = parser.getXMLReader();

    BlogEmotionsHandler handler = new BlogEmotionsHandler();

    //设置Handler

    reader.setContentHandler(handler);

    //指定文件,进行解析

    reader.parse(new InputSource(new StringReader(result)));   
    quals(localName)) {

responseresult = new ResponseResult();

} else if (“emotions”.equals(localName)) {

} else if (“emotion”.equals(localName)) {

emotions = new Emotions();

}

tag = localName;

}

}

3.4sax解析

Java代码  

    public List getEmotion(){

    BlogGetData getdata = new BlogGetData();

    String result = getdata.blogEmotionsServlet();

    try {

    //生成SAX解析对象

    parser = SAXParserFactory.newInstance().newSAXParser();

    //生成xml读取器

    reader = parser.getXMLReader();

    BlogEmotionsHandler handler = new BlogEmotionsHandler();

    //设置Handler

    reader.setContentHandler(handler);

    //指定文件,进行解析

    reader.parse(new InputSource(new StringReader(result)));

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/720564.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号