在上面一篇中已经实现了预读微博的UI界面,效果如上图,接下来完成功能部分的代码,当用户在上一个列表界面的列表中点击某一条微博的时候显示这个阅读微博的界面,在这个界面中根据传来的微博ID,然后根据这个ID通过api获取微博的具体内容进行显示。
在ViewActivity.class的onCreate方法中添加如下代码:
- private UserInfo user;
- private String key="";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.view);
-
- 。。。。。
-
- //获取上一个页面传递过来的key,key为某一条微博的id
- Intent i=this.getIntent();
- if(!i.equals(null)){
- Bundle b=i.getExtras();
- if(b!=null){
- if(b.containsKey("key")){
- key = b.getString("key");
- view(key);
- }
- }
- }
- }
复制代码 接下来就是view方法具体获取微博内容的方法,在这个方法中如果获取的本条微博如果包含图片那么就用前面AsyncImageLoader的方法异步载入图片并且进行显示,同时在这个方法中还要获取本条微博被转发的次数以及评论的次数,具体代码如下:
- private void view(String id){
- user=ConfigHelper.nowUser;
- OAuth auth=new OAuth();
- String url = "";
- List params=new ArrayList();
- params.add(new BasicNameValuePair("source", auth.consumerKey));
- params.add(new BasicNameValuePair("id", id));
- HttpResponse response =auth.SignRequest(user.getToken(), user.getTokenSecret(), url, params);
- if (200 == response.getStatusLine().getStatusCode()){
- try {
- InputStream is = response.getEntity().getContent();
- Reader reader = new BufferedReader(new InputStreamReader(is), 4000);
- StringBuilder buffer = new StringBuilder((int) response.getEntity().getContentLength());
- try {
- char[] tmp = new char[1024];
- int l;
- while ((l = reader.read(tmp)) != -1) {
- buffer.append(tmp, 0, l);
- }
- } finally {
- reader.close();
- }
- String string = buffer.toString();
- //Log.e("json", "rs:" + string);
- response.getEntity().consumeContent();
- JSONObject data=new JSONObject(string);
- if(data!=null){
- JSONObject u=data.getJSONObject("user");
- String userName=u.getString("screen_name");
- String userIcon=u.getString("profile_image_url");
- Log.e("userIcon", userIcon);
- String time=data.getString("created_at");
- String text=data.getString("text");
-
- TextView utv=(TextView)findViewById(R.id.user_name);
- utv.setText(userName);
- TextView ttv=(TextView)findViewById(R.id.text);
- ttv.setText(text);
-
- ImageView iv=(ImageView)findViewById(R.id.user_icon);
- AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
- Drawable cachedImage = asyncImageLoader.loadDrawable(userIcon,iv, new ImageCallback(){
- @Override
- public void imageLoaded(Drawable imageDrawable,ImageView imageView, String imageUrl) {
-
- imageView.setImageDrawable(imageDrawable);
- }
- });
- if (cachedImage == null)
- {
- iv.setImageResource(R.drawable.usericon);
- }
- else
- {
- iv.setImageDrawable(cachedImage);
- }
- if(data.has("bmiddle_pic")){
- String picurl=data.getString("bmiddle_pic");
- String picurl2=data.getString("original_pic");
- ImageView pic=(ImageView)findViewById(R.id.pic);
- pic.setTag(picurl2);
- pic.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Object obj=v.getTag();
- Intent intent = new Intent(ViewActivity.this,ImageActivity.class);
- Bundle b=new Bundle();
- b.putString("url", obj.toString());
- intent.putExtras(b);
- startActivity(intent);
- }
- });
- Drawable cachedImage2 = asyncImageLoader.loadDrawable(picurl,pic, new ImageCallback(){
- @Override
- public void imageLoaded(Drawable imageDrawable,ImageView imageView, String imageUrl) {
- showImg(imageView,imageDrawable);
- }
- });
- if (cachedImage2 == null)
- {
- //pic.setImageResource(R.drawable.usericon);
- }
- else
- {
- showImg(pic,cachedImage2);
- }
- }
- }
- }catch (IllegalStateException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- url = "";
- params=new ArrayList();
- params.add(new BasicNameValuePair("source", auth.consumerKey));
- params.add(new BasicNameValuePair("ids", id));
- response =auth.SignRequest(user.getToken(), user.getTokenSecret(), url, params);
- if (200 == response.getStatusLine().getStatusCode()){
- try {
- InputStream is = response.getEntity().getContent();
- Reader reader = new BufferedReader(new InputStreamReader(is), 4000);
- StringBuilder buffer = new StringBuilder((int) response.getEntity().getContentLength());
- try {
- char[] tmp = new char[1024];
- int l;
- while ((l = reader.read(tmp)) != -1) {
- buffer.append(tmp, 0, l);
- }
- } finally {
- reader.close();
- }
- String string = buffer.toString();
- response.getEntity().consumeContent();
- JSONArray data=new JSONArray(string);
- if(data!=null){
- if(data.length()>0){
- JSONObject d=data.getJSONObject(0);
- String comments=d.getString("comments");
- String rt=d.getString("rt");
- Button btn_gz=(Button)findViewById(R.id.btn_gz);
- btn_gz.setText(" 转发("+rt+")");
- Button btn_pl=(Button)findViewById(R.id.btn_pl);
- btn_pl.setText(" 评论("+comments+")");
- }
- }
- }
- catch (IllegalStateException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }
复制代码 在上面的方法中对于微博中包含的图片显示尺寸进行了特别的处理,如果直接把获取的图片显示在ImageView中,因为当图片宽高超过手机屏幕的时候,系统会自动按照手机的屏幕按比例缩放图片进行显示,但是我发现一个现象图片的高虽然是按照比例缩小了,但是图片占据的高仍旧是原来图片的高度照成真实图片和文字内容之间多了很高的一块空白,这个现象非常的奇怪,所以我写了如下方法进行处理:
- private void showImg(ImageView view,Drawable img){
- int w=img.getIntrinsicWidth();
- int h=img.getIntrinsicHeight();
- Log.e("w", w+"/"+h);
- if(w>300)
- {
- int hh=300*h/w;
- Log.e("hh", hh+"");
- LayoutParams para=view.getLayoutParams();
- para.width=300;
- para.height=hh;
- view.setLayoutParams(para);
- }
- view.setImageDrawable(img);
- }
复制代码 本篇到这里就结束了,请继续关注下一篇。
阅读(954) | 评论(0) | 转发(0) |