陈桥驿站 陈桥驿站

Android课程设计:新闻客户端

Android 课程设计 阅读 ( 324 ) 陈桥驿站

简介

基于Android的新闻客户端,采用Struts+Hibernate为Service,Client和Server之间通过HTTP协议传输。主要包含登录注册、分类搜索、模糊搜索、收藏文章等功能。[AndroidMyNewsApplication.zip]

<video src="../../../media/AndroidNews.mp4"></video>

代码

登录注册

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Intent intent = new Intent();
    if(item.getItemId() == R.id.menu_like){
        //收藏
        if(MainActivity.online == null || MainActivity.online.equals("")){
            Toast.makeText(this, "然而你木有登录-_-||", Toast.LENGTH_LONG).show();
        }
        else{
            intent.setClass(this, ListActivity.class);
            intent.putExtra("action", "queryNewsLike.action?i=" + MainActivity.online);
            startActivity(intent);
        }
    }
    else if(item.getItemId() == R.id.menu_login){
        //登录
        intent.setClass(this, LoginActivity.class);
        startActivity(intent);
    }
    return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.login_text:
            //创建账号
            Intent intent = new Intent(this, RegisterActivity.class);
            startActivity(intent);
            break;

        case R.id.login_button:
            //登录
            if(StringUtils.empty(id) || StringUtils.empty(password)){
                Toast.makeText(LoginActivity.this, "填完邮箱和密码方可登录", Toast.LENGTH_LONG).show();
            }
            else{
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Message message = new Message();
                        message.what = 1;
                        message.obj = new StringUtils().get("queryNewsUser.action?id=" + id.getText().toString() + "&password=" + password.getText().toString());
                        handler.sendMessage(message);
                    }
                }).start();
            }
            break;
    }
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    //EditText
    password = (EditText) findViewById(R.id.register_password);
    confirm = (EditText) findViewById(R.id.register_confirm);
    id = (EditText) findViewById(R.id.register_email);
    name = (EditText) findViewById(R.id.register_name);
    findViewById(R.id.register_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(StringUtils.empty(id) || StringUtils.empty(name) || StringUtils.empty(password) || StringUtils.empty(confirm)){
                Toast.makeText(RegisterActivity.this, "填完所有信息后方可注册", Toast.LENGTH_LONG).show();
            }
            else if(!(password.getText().toString().equals(confirm.getText().toString()))){
                Toast.makeText(RegisterActivity.this, "两次密码输入一致方可注册", Toast.LENGTH_LONG).show();
            }
            else{
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String string = "updateNewsUser.action?" + "newsUser.name=" + name.getText()
                                + "&newsUser.id=" + id.getText() + "&newsUser.password=" + password.getText();
                        Message message = new Message();
                        message.what = 1;
                        message.obj = new StringUtils().get(string);
                        handler.sendMessage(message);
                    }
                }).start();
            }
        }
    });
}

新闻列表

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(ListActivity.this, DetailActivity.class);
        intent.putExtra("url", newsAlls.get(position).getUrl());
        intent.putExtra("id", newsAlls.get(position).getId());
        startActivity(intent);
    }
});

收藏新闻

like.setOnLikeListener(new OnLikeListener() {
    @Override
    public void liked(LikeButton likeButton) {
        if(MainActivity.online == null || MainActivity.online.equals("")){
            Toast.makeText(DetailActivity.this, "木有登录不能收藏-_-||", Toast.LENGTH_LONG).show();
        }
        else{
            like.setEnabled(false);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String url = "updateNewsLike.action?newsLike.id=" + UUID.randomUUID() + "&newsLike.i=" + MainActivity.online + "&newsLike.parent=" + getIntent().getExtras().getInt("id");
                    Message message = new Message();
                    message.what = 1;
                    message.obj = new StringUtils().get(url);
                    handler.sendMessage(message);
                }
            }).start();
        }
    }

    @Override
    public void unLiked(LikeButton likeButton) {

    }
});

新闻详情

WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl(getIntent().getExtras().getString("url"));
webView.setWebViewClient(new WebViewClient(){
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
});
jrotty WeChat Pay

微信打赏

jrotty Alipay

支付宝打赏

文章二维码

扫描二维码,在手机上阅读!

本文基于《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权
文章链接:http://www.cctv3.net/archives/AndroidNews.html (转载时请注明本文出处及文章链接)

Android课程设计
发表新评论
陈桥驿站已萌萌哒运行
© 2020百度云提供云加速服务 阿里云提供域名解析和云计算服务 鲁ICP备18036467号-2
PREVIOUS NEXT
雷姆
拉姆