匿名项目连载(四)--->评论说说
2015-08-24 14:40
295 查看
AtyMessage
package com.jikexueyuan.secret.atys; import java.util.List; import com.jikexueyuan.secret.Config; import com.jikexueyuan.secret.R; import com.jikexueyuan.secret.net.Comment; import com.jikexueyuan.secret.net.GetComment; import com.jikexueyuan.secret.net.PubComment; import com.jikexueyuan.secret.tools.MD5Tool; import android.app.ListActivity; import android.app.ProgressDialog; import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class AtyMessage extends ListActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.aty_message); adapter = new AtyMessageCommentListAdapter(this); setListAdapter(adapter); tvMessage = (TextView) findViewById(R.id.tvMessage); etComment = (EditText) findViewById(R.id.etComment); Intent data = getIntent(); phone_md5 = data.getStringExtra(Config.KEY_PHONE_MD5); msg = data.getStringExtra(Config.KEY_MSG); msgId = data.getStringExtra(Config.KEY_MSG_ID); token = data.getStringExtra(Config.KEY_TOKEN); tvMessage.setText(msg); /** * 从服务器获取该条说说的评论 */ getComments(); findViewById(R.id.btnSendComment).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { //非空判断 if (TextUtils.isEmpty(etComment.getText())) { Toast.makeText(AtyMessage.this, R.string.comment_content_can_not_be_empty, Toast.LENGTH_LONG).show(); return; } //进度条提示 final ProgressDialog pd = ProgressDialog.show(AtyMessage.this, getResources().getString(R.string.connecting), getResources().getString(R.string.connecting_to_server)); /** * 发布消息评论至服务器 */ new PubComment(MD5Tool.md5(Config.getCachedPhoneNum(AtyMessage.this)), token, etComment.getText().toString(), msgId, new PubComment.SuccessCallback() { @Override public void onSuccess() { pd.dismiss(); etComment.setText(""); //从服务器获取说说评论 getComments(); } }, new PubComment.FailCallback() { @Override public void onFail(int errorCode) { pd.dismiss(); if (errorCode==Config.RESULT_STATUS_INVALID_TOKEN) { startActivity(new Intent(AtyMessage.this, AtyLogin.class)); finish(); }else{ Toast.makeText(AtyMessage.this, R.string.fail_to_pub_comment, Toast.LENGTH_LONG).show(); } } }); } }); } /** * 从服务器获取说说评论 */ private void getComments() { //进度条提示 final ProgressDialog pd = ProgressDialog.show(this, getResources().getString(R.string.connecting), getResources().getString(R.string.connecting_to_server)); //从服务器获取消息评论 new GetComment(phone_md5, token, msgId, 1, 20, new GetComment.SuccessCallback() { @Override public void onSuccess(String msgId, int page, int perpage, List<Comment> comments) { pd.dismiss(); adapter.clear(); adapter.addAll(comments); } }, new GetComment.FailCallback() { @Override public void onFail(int errorCode) { pd.dismiss(); if (errorCode==Config.RESULT_STATUS_INVALID_TOKEN) { startActivity(new Intent(AtyMessage.this, AtyLogin.class)); finish(); }else{ Toast.makeText(AtyMessage.this, R.string.fail_to_get_comment, Toast.LENGTH_LONG).show(); } } }); } private TextView tvMessage; private EditText etComment; private String phone_md5,msg,msgId,token; private AtyMessageCommentListAdapter adapter; }
new PubComment(phone_md5, token, content, msgId, successCallback, failCallback)的方法实现类如下
package com.jikexueyuan.secret.net; import org.json.JSONException; import org.json.JSONObject; import com.jikexueyuan.secret.Config; public class PubComment { public PubComment(String phone_md5,String token,String content,String msgId,final SuccessCallback successCallback,final FailCallback failCallback) { new NetConnection(Config.SERVER_URL, HttpMethod.POST, new NetConnection.SuccessCallback() { @Override public void onSuccess(String result) { try { //{\"status\":1}只返回状态值 JSONObject obj = new JSONObject(result); switch (obj.getInt(Config.KEY_STATUS)) { case Config.RESULT_STATUS_SUCCESS: if (successCallback!=null) { successCallback.onSuccess(); } break; case Config.RESULT_STATUS_INVALID_TOKEN: if (failCallback!=null) { failCallback.onFail(Config.RESULT_STATUS_INVALID_TOKEN); } break; default: if (failCallback!=null) { failCallback.onFail(Config.RESULT_STATUS_FAIL); } break; } } catch (JSONException e) { e.printStackTrace(); if (failCallback!=null) { failCallback.onFail(Config.RESULT_STATUS_FAIL); } } } }, new NetConnection.FailCallback() { @Override public void onFail() { if (failCallback!=null) { failCallback.onFail(Config.RESULT_STATUS_FAIL); } } }, Config.KEY_ACTION,Config.ACTION_PUB_COMMENT, Config.KEY_TOKEN,token, Config.KEY_PHONE_MD5,phone_md5, Config.KEY_MSG_ID,msgId); //action=pub_comment&token=asdasdasdsadasd&phone_md5=96e79218965eb72c92a549dd5a330112&msgId=1231234& } public static interface SuccessCallback{ void onSuccess(); } public static interface FailCallback{ void onFail(int errorCode); } }
new GetComment(phone_md5, token, msgId, page, perpage, successCallback, failCallback)的方法实现类如下
Comment
package com.jikexueyuan.secret.net; public class Comment { public Comment(String content,String phone_md5) { this.content = content; this.phone_md5 = phone_md5; } private String content,phone_md5; public String getContent() { return content; } public String getPhone_md5() { return phone_md5; } }
package com.jikexueyuan.secret.net; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import com.jikexueyuan.secret.Config; public class GetComment { public GetComment(String phone_md5,String token,String msgId,int page,int perpage,final SuccessCallback successCallback,final FailCallback failCallback) { new NetConnection(Config.SERVER_URL, HttpMethod.POST, new NetConnection.SuccessCallback() { @Override public void onSuccess(String result) { /** * out.print("{\"status\":1,\"page\":1,\"perpage\":20,\"msgId\":\"12133\",\"comments\":[" + "{\"content\":\"Hehe1\",\"phone_md5\":\"dasdasdbkas\"}," + "{\"content\":\"Hehe2\",\"phone_md5\":\"dasdasdbkas\"}," + "{\"content\":\"Hehe3\",\"phone_md5\":\"dasdasdbkas\"}," + "{\"content\":\"Hehe4\",\"phone_md5\":\"dasdasdbkas\"}," + "{\"content\":\"Hehe5\",\"phone_md5\":\"dasdasdbkas\"}," + "{\"content\":\"Hehe6\",\"phone_md5\":\"dasdasdbkas\"}," + "{\"content\":\"Hehe7\",\"phone_md5\":\"dasdasdbkas\"}," + "{\"content\":\"Hehe8\",\"phone_md5\":\"dasdasdbkas\"}" + "]}"); */ try { JSONObject jsonObject = new JSONObject(result); switch (jsonObject.getInt(Config.KEY_STATUS)) { case Config.RESULT_STATUS_SUCCESS: if (successCallback!=null) { List<Comment> comments = new ArrayList<Comment>(); JSONArray commentsJsonArray =jsonObject.getJSONArray(Config.KEY_COMMENTS); JSONObject commentObj; for (int i = 0; i < commentsJsonArray.length(); i++) { commentObj = commentsJsonArray.getJSONObject(i); comments.add(new Comment(commentObj.getString(Config.KEY_CONTENT), commentObj.getString(Config.KEY_PHONE_MD5))); } successCallback.onSuccess(jsonObject.getString(Config.KEY_MSG_ID), jsonObject.getInt(Config.KEY_PAGE), jsonObject.getInt(Config.KEY_PERPAGE), comments); // successCallback.onSuccess("123", 1, 20, comments); } break; case Config.RESULT_STATUS_INVALID_TOKEN: if (failCallback!=null) { failCallback.onFail(Config.RESULT_STATUS_INVALID_TOKEN); } break; default: if (failCallback!=null) { failCallback.onFail(Config.RESULT_STATUS_FAIL); } break; } } catch (JSONException e) { e.printStackTrace(); if (failCallback!=null) { failCallback.onFail(Config.RESULT_STATUS_FAIL); } } } }, new NetConnection.FailCallback() { @Override public void onFail() { if (failCallback!=null) { failCallback.onFail(Config.RESULT_STATUS_FAIL); } } }, Config.KEY_ACTION,Config.ACTION_GET_COMMENT, Config.KEY_TOKEN,token, Config.KEY_MSG_ID,msgId, Config.KEY_PAGE,page+"", Config.KEY_PERPAGE,perpage+""); } public static interface SuccessCallback{ void onSuccess(String msgId,int page,int perpage,List<Comment> comments); } public static interface FailCallback{ void onFail(int errorCode); } }
获取数据完毕后,进行页面填充数据
adapter.clear();
adapter.addAll(comments);实现类如下
AtyMessageCommentListAdapter
package com.jikexueyuan.secret.atys; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.jikexueyuan.secret.R; import com.jikexueyuan.secret.net.Comment; public class AtyMessageCommentListAdapter extends BaseAdapter { public AtyMessageCommentListAdapter(Context context) { this.context = context; } @Override public int getCount() { return comments.size(); } @Override public Comment getItem(int arg0) { return comments.get(arg0); } @Override public long getItemId(int arg0) { return arg0; } public void addAll(List<Comment> data){ comments.addAll(data); notifyDataSetChanged(); } public void clear(){ comments.clear(); notifyDataSetChanged(); } @Override public View getView(int arg0, View convertView, ViewGroup arg2) { if (convertView==null) { convertView = LayoutInflater.from(getContext()).inflate(R.layout.aty_timeline_list_cell, null); convertView.setTag(new ListCell((TextView) convertView.findViewById(R.id.tvCellLabel))); } ListCell lc = (ListCell) convertView.getTag(); Comment comment = getItem(arg0); lc.getTvCellLabel().setText(comment.getContent()); return convertView; } private List<Comment> comments = new ArrayList<Comment>(); private Context context; public Context getContext() { return context; } private static class ListCell{ public ListCell(TextView tvCellLabel) { this.tvCellLabel = tvCellLabel; } private TextView tvCellLabel; public TextView getTvCellLabel() { return tvCellLabel; } } }
相关文章推荐
- 语言分类
- android学习记录(1)——搭建环境(win x86)
- 解决Adobe Acrobat x Pro序列号无效/重新激活方法
- AWS发布架构师认证的专业解决方案
- 如何做个好员工
- Ubuntu11.04安装、配置samba
- Android-Fragment(生命周期)
- 报表引擎API开发入门—简单程序数据集
- CF#315 C
- MP3头部结构体定义(一)
- 报表引擎API开发入门—简单程序数据集
- 【转】Could not open Selected VM debug port (8700)
- Web 实时推送技术的总结
- iOS深入学习(Block全面分析)
- 如何发现交互设计中的思维盲区
- eclipce 安装 svn插件(百度知道)
- js监听文本框变化事件
- Extjs4 之单元格编辑 获取列的对象
- package 和 import 关键字
- 用Quartz处理定时执行的任务