package com.example.nbwb;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import com.example.util.JsonParpser;
import com.example.util.StringUtil;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class NBMainActivity extends BaseActivity {
private TextView username;
private Button manager;
private String json, permissions, uname, userid, bid, bnum,fatten;
private ListView lv;
private List<Map<String, String>> blogs;
private SimpleAdapter sa;
private Handler handler;
private static final int REFER = 0, ATTENTION = 1, COLLECT = 2, REVIEW = 3,
LOOKREVIEW = 4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nbmain);
getButton();
// 加载信息
username = (TextView) findViewById(R.id.username);
manager = (Button) findViewById(R.id.button1);
SharedPreferences pres = NBMainActivity.this.getSharedPreferences(
"user", Context.MODE_PRIVATE);
permissions = pres.getString("permissions", "");
uname = pres.getString("uname", "");
userid=pres.getString("userid", "");
username.setText(uname);
if ("1".equals(permissions)) {
manager.setVisibility(View.VISIBLE);
}
// 微博获取
lv = (ListView) findViewById(R.id.listViewBlogs);
String str = "[{\"bid\":\"0\",\"userid\":\"0\",\"uname\":\"代老大\",\"btext\":\"好好学习,天天向上!\","
+ "\"btime\":\"刚刚\",\"bnum\":\"0\"}]";
blogs = JsonParpser.parserJson(str);
sa = new SimpleAdapter(this, blogs, R.layout.item_blogs, new String[] {
"uname", "btext", "btime", "bnum" }, new int[] { R.id.buname,
R.id.btext, R.id.btime, R.id.r_num });
lv.setAdapter(sa);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
Bundle data = msg.getData();
String json = data.getString("json");
switch (msg.what) {
case REFER:
blogs.clear();
blogs.addAll(JsonParpser.parserJson(json));
sa.notifyDataSetChanged();
break;
case ATTENTION:
if ("1".equals(json)) {
Toast.makeText(NBMainActivity.this, "关注成功", Toast.LENGTH_SHORT).show();
}else if("2".equals(json)){
Toast.makeText(NBMainActivity.this, "已关注", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(NBMainActivity.this, "关注失败", Toast.LENGTH_SHORT).show();
}
break;
case COLLECT:
if ("1".equals(json)) {
Toast.makeText(NBMainActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
}else if("2".equals(json)){
Toast.makeText(NBMainActivity.this, "已收藏", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(NBMainActivity.this, "收藏失败", Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
};
// 添加长按事件
registerForContextMenu(lv);
}
// 处理菜单事件
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
AdapterContextMenuInfo acm = (AdapterContextMenuInfo) item
.getMenuInfo();
Map<String, String> value = (Map<String, String>) lv
.getItemAtPosition(acm.position);
bid = value.get("bid");
bnum = value.get("bnum");
fatten=value.get("userid");
String name = value.get("uname");
switch (item.getItemId()) {
case 1:
if (name.equals(uname)) {
Toast.makeText(NBMainActivity.this, "对不起,不能关注自己", Toast.LENGTH_SHORT).show();
}else {
new Thread(){
@Override
public void run() {
String url="http://expect-xh.oicp.net:8080/NBwb/FriendServlet?function=addFriend&userid="+userid+"&fattentionid="+fatten;//我的IP
HttpURLConnection con=null;
InputStream is=null;
try {
URL address=new URL(url);
con = (HttpURLConnection) address.openConnection();
con.setConnectTimeout(10000);
is = con.getInputStream();
json = StringUtil.readStream(is);
//
Log.i("haolei", json);
Message msg=new Message();
Bundle data=new Bundle();
data.putString("json", json);
msg.setData(data);
msg.what=ATTENTION;
handler.sendMessage(msg);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
break;
case 2:
if (name.equals(uname)) {
Toast.makeText(NBMainActivity.this, "对不起,不能收藏自己的微博", Toast.LENGTH_SHORT).show();
}else {
new Thread(){
@Override
public void run() {
String url="http://expect-xh.oicp.net:8080/NBwb/CollectServlet?function=addCollect&userid="+userid+"&bid="+bid;
HttpURLConnection con=null;
InputStream is=null;
try {
URL address=new URL(url);
con = (HttpURLConnection) address.openConnection();
con.setConnectTimeout(10000);
is = con.getInputStream();
json = StringUtil.readStream(is);
Log.i("haolei", json);
Message msg=new Message();
Bundle data=new Bundle();
data.putString("json", json);
msg.setData(data);
msg.what=COLLECT;
handler.sendMessage(msg);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
break;
case 3:
Intent intent=new Intent();
intent.putExtra("userid", userid);
intent.putExtra("bid", bid);
intent.setClass(this, SendReviewActivity.class);
startActivity(intent);
break;
case 4:
if ("0".equals(bnum)) {
Toast.makeText(NBMainActivity.this,"对不起,此微博无评论。", Toast.LENGTH_SHORT).show();
}else {
Intent i=new Intent();
i.putExtra("bid", bid);
i.setClass(this, LookReviewActivity.class);
startActivity(i);
}
break;
default:
break;
}
return true;
}
// 长按菜单
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(1, 1, 1, R.string.attention);
menu.add(1, 2, 1, R.string.collect);
menu.add(1, 3, 1, R.string.review);
menu.add(1, 4, 1, R.string.lookreview);
}
public void refersh(View view) {
new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
String url = "http://expect-xh.oicp.net:8080/NBwb/BlogServlet?function=findBlogAll";
HttpURLConnection con = null;
InputStream is = null;
try {
URL address = new URL(url);
con = (HttpURLConnection) address.openConnection();
con.setConnectTimeout(10000);
is = con.getInputStream();
json = StringUtil.readStream(is);
// Log.i("huoqu", json);
Message msg = new Message();
Bundle data = new Bundle();
data.putString("json", json);
msg.setData(data);
msg.what = REFER;
handler.sendMessage(msg);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
public void manager(View view){
//
Toast.makeText(this, "manager", Toast.LENGTH_SHORT).show();
Intent intent=new Intent(this,ManagerActivity.class);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.nbmain, menu);
return true;
}
}
相关文章
- requirejs加载bootstrap报错处理:Bootstrap dropdown require Popper.js (https://popper.js.org)
- 处理Angular报错:ERROR in Metadata version mismatch for module
- 麻了,代码改成多线程,服务直接挂了
- JUC并发编程第二章之CompletableFuture[加强版的线程]
- 7种实现web实时消息推送的方案
- Java多线程:线程池
- 【Spring原理分析-Bean生命周期+Spring原理分析-Bean后处理器】
- Pytorch深度学习实战3-7:详解数据加载DataLoader与模型处理
- Java 定时线程池各个方法的区别 Java周期执行线程池各个方法的区别 ScheduledExecutorService schedulerService
- BeanShell处理返回报文写一个TXT文档