最近练习一些关于发布帖子的技术,说来也简单,就学了一点皮毛吧!好了,下面就上代码吧!

首先设计服务器的访问类,大家都知道现在东西都要联网的嘛!

JSONParser的类:

public class JSONParser{
static InputStream is = null;
static String json="";
public static String PHPSESSID=null;

public JSONParser(){

}
public String makeHttpRequest(String url,String method,List<NameValuePair> params)
{
try
{
HttpPost httpPost=new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

if(null!=PHPSESSID)
{
httpPost.setHeader("Cookie","PHPSESSID="+PHPSESSID);
}

DefaultHttpClient httpClient=new DefaultHttpClient();

HttpResponse httpResponse=httpClient.execute(httpPost);
HttpEntity httpEntity=httpResponse.getEntity();

is=httpEntity.getContent();//返回值

CookieStore mCookieStore=httpClient.getCookieStore();
List<Cookie> cookies=mCookieStore.getCookies();
for(int i=0;i<cookies.size();i++){
if("PHPSESSID".equals(cookies.get(i).getName())){
PHPSESSID=cookies.get(i).getValue();
break;
}
}
}catch(UnsupportedEncodingException e){
e.printStackTrace();
}catch(ClientProtocolException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
try{
//从JSON输入流中读取信息
BufferedReader reader=new BufferedReader(new InputStreamReader(is,"UTF-8"));

StringBuilder sb=new StringBuilder();
String line=null;

while((line=reader.readLine())!=null){
sb.append(line+"\n");
}

is.close();
json=sb.toString();//把JSON对象转换成为字符串
}
catch(Exception e)
{
e.printStackTrace();
}
return json;

}

这是发布帖子的关键代码:

class Sendmsg extends AsyncTask<String, String, String>{

protected void onPreExecute() {
super.onPreExecute();
pdialog=new ProgressDialog(Main_Send.this);
pdialog.setMessage("正在发表...");
pdialog.setIndeterminate(false);
pdialog.setCancelable(true);
pdialog.show();
}

protected String doInBackground(String... arg0) {

List<NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("text_title", text_title));
params.add(new BasicNameValuePair("text_content", text_content));
params.add(new BasicNameValuePair("text_zuozhe", text_zuozhe));
try{
jsonData=jsonpaeser.makeHttpRequest(url, "POST", params);
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}

这是获取帖子的关键代码,采用的是json的解析:

class Getmsg extends AsyncTask<String, String, String>{

protected String doInBackground(String... arg0) {
notes.clear();
jsonData=callgetnotice.makeHttpRequest();
try{
JSONArray array=new JSONArray(jsonData);
Note_Entity noteentity;

for(int i=0;i<array.length();i++){
noteentity=new Note_Entity();
JSONObject json=array.getJSONObject(i);
noteentity.setId(json.getString("id"));
noteentity.setTitle(json.getString("title"));
noteentity.setContent(json.getString("content"));
noteentity.setZuozhe(json.getString("zuozhe"));
noteentity.setDate(json.getString("date"));
noteentity.setTime(json.getString("time"));
notes.add(noteentity);
}

}catch(Exception e)
{
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result) {
adapter=new NoteAdapter(notes, Main_Note.this);
listview.setAdapter(adapter);
}
}

以上就是关于帖子的发布以及获取帖子的关键代码,只要清楚的理解代码,想必你也可以弄出来的。

采用都是异步发布,异步获取,然后用listview显示出来。数据库方面,就是几个库,自己弄得还是比较粗糙和简单。接下来打算学一下下拉刷新,还有图文等等。

最新文章

  1. .NET 4.0 中超长路径超长文件名的问题
  2. Spring 常用的一些工具类
  3. .Net语言 APP开发平台——Smobiler学习日志:用Gridview控件设计较复杂的表单
  4. spring Transaction Propagation 事务传播
  5. [整理][LaTex]小技巧之——首行缩进
  6. OC学习笔记——类别(Category)
  7. C# 之 HttpWebRequest类
  8. ViewPager循环广告位的实现
  9. Android平台抓取native crash log
  10. Docker学习计划
  11. swift学习 - tableView自适应高度1(xib autoLayout)
  12. 【HNOI2004】宠物收养所(splay)
  13. CRM客户关系管理系统(十二)
  14. redis简介与持久化
  15. Linux 内存清理
  16. 002.Kickstart部署之NFS架构
  17. Weighted Quick Union
  18. 平衡树Splay
  19. SQL中 根据行号设置每行数据的排序数值
  20. Winform鼠标滑轮控制自定义滚动条

热门文章

  1. JSP初学者4
  2. ArcGIS Enterprise 10.5.1 静默安装部署记录(Centos 7.2 minimal)- 2、安装WebAdapter
  3. dos.ORM配置和使用
  4. April 2 2017 Week 14 Sunday
  5. CSS基础语法(三) CSS的6种特性
  6. POJ 3308 最少点集覆盖
  7. Ubuntu不支持rpm安装软件解决方法
  8. Services 在多个 controller 中共享数据。
  9. GPU卡掉卡
  10. linux 使用sqlite3