匿名项目连载(四)--->评论说说

来源:互联网 发布:php设计表格 编辑:程序博客网 时间:2024/06/09 13:45

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{@Overrideprotected 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() {@Overridepublic 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() {@Overridepublic void onSuccess() {pd.dismiss();etComment.setText("");//从服务器获取说说评论getComments();}}, new PubComment.FailCallback() {@Overridepublic 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() {@Overridepublic void onSuccess(String msgId, int page, int perpage,List<Comment> comments) {pd.dismiss();adapter.clear();adapter.addAll(comments);}}, new GetComment.FailCallback() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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;}@Overridepublic int getCount() {return comments.size();}@Overridepublic Comment getItem(int arg0) {return comments.get(arg0);}@Overridepublic long getItemId(int arg0) {return arg0;}public void addAll(List<Comment> data){comments.addAll(data);notifyDataSetChanged();}public void clear(){comments.clear();notifyDataSetChanged();}@Overridepublic 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;}}}


0 0
原创粉丝点击