基本信息
源码名称:android 仿制qq的简化版聊天,加入聊天机器人
源码大小:2.77M
文件格式:.zip
开发语言:Java
更新时间:2017-01-09
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
public class HttpUtils
{
private static String API_KEY = "ab391a7ee27b4416941c42119bf9dab1";
private static String URL = "http://www.tuling123.com/openapi/api";
/**
* 发送一个消息,并得到返回的消息
* @param msg
* @return
*/
public static ChatMessage sendMsg(String msg)
{
ChatMessage message = new ChatMessage();
String url = setParams(msg);
String res = doGet(url);
Gson gson = new Gson();
Result result = gson.fromJson(res, Result.class);
if (result.getCode() > 400000 || result.getText() == null
|| result.getText().trim().equals(""))
{
message.setMsg("该功能等待开发...");
}else
{
message.setMsg(result.getText());
}
message.setType(com.example.xiao001.ChatMessage.Type.INPUT);
message.setDate(new Date());
return message;
}
/**
* 拼接Url
* @param msg
* @return
*/
private static String setParams(String msg)
{
try
{
msg = URLEncoder.encode(msg, "UTF-8");
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
return URL "?key=" API_KEY "&info=" msg;
}
/**
* Get请求,获得返回数据
* @param urlStr
* @return
*/
private static String doGet(String urlStr)
{
URL url = null;
HttpURLConnection conn = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try
{
url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(5 * 1000);
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200)
{
is = conn.getInputStream();
baos = new ByteArrayOutputStream();
int len = -1;
byte[] buf = new byte[128];
while ((len = is.read(buf)) != -1)
{
baos.write(buf, 0, len);
}
baos.flush();
return baos.toString();
} else
{
throw new CommonException("服务器连接错误!");
}
} catch (Exception e)
{
e.printStackTrace();
throw new CommonException("服务器连接错误!");
} finally
{
try
{
if (is != null)
is.close();
} catch (IOException e)
{
e.printStackTrace();
}
try
{
if (baos != null)
baos.close();
} catch (IOException e)
{
e.printStackTrace();
}
conn.disconnect();
}
}
}
listview嵌套列表,聊天机器人,一个简单的聊天软件,账号:xiaoluozi 密码:123456
public class HttpUtils
{
private static String API_KEY = "ab391a7ee27b4416941c42119bf9dab1";
private static String URL = "http://www.tuling123.com/openapi/api";
/**
* 发送一个消息,并得到返回的消息
* @param msg
* @return
*/
public static ChatMessage sendMsg(String msg)
{
ChatMessage message = new ChatMessage();
String url = setParams(msg);
String res = doGet(url);
Gson gson = new Gson();
Result result = gson.fromJson(res, Result.class);
if (result.getCode() > 400000 || result.getText() == null
|| result.getText().trim().equals(""))
{
message.setMsg("该功能等待开发...");
}else
{
message.setMsg(result.getText());
}
message.setType(com.example.xiao001.ChatMessage.Type.INPUT);
message.setDate(new Date());
return message;
}
/**
* 拼接Url
* @param msg
* @return
*/
private static String setParams(String msg)
{
try
{
msg = URLEncoder.encode(msg, "UTF-8");
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
return URL "?key=" API_KEY "&info=" msg;
}
/**
* Get请求,获得返回数据
* @param urlStr
* @return
*/
private static String doGet(String urlStr)
{
URL url = null;
HttpURLConnection conn = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try
{
url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(5 * 1000);
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200)
{
is = conn.getInputStream();
baos = new ByteArrayOutputStream();
int len = -1;
byte[] buf = new byte[128];
while ((len = is.read(buf)) != -1)
{
baos.write(buf, 0, len);
}
baos.flush();
return baos.toString();
} else
{
throw new CommonException("服务器连接错误!");
}
} catch (Exception e)
{
e.printStackTrace();
throw new CommonException("服务器连接错误!");
} finally
{
try
{
if (is != null)
is.close();
} catch (IOException e)
{
e.printStackTrace();
}
try
{
if (baos != null)
baos.close();
} catch (IOException e)
{
e.printStackTrace();
}
conn.disconnect();
}
}
}