首页 > 技术文章 > android发布帖子类技术

jiehong 2017-06-15 14:50 原文

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

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

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显示出来。数据库方面,就是几个库,自己弄得还是比较粗糙和简单。接下来打算学一下下拉刷新,还有图文等等。

 

推荐阅读