Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> android開發我的新浪微博客戶端-閱讀微博功能篇(6.2)

android開發我的新浪微博客戶端-閱讀微博功能篇(6.2)

日期:2017/2/24 17:33:16      編輯:Android開發實例

 

注:最近由於OAuth上傳圖片碰到了難題,一直在做這方面的研究導致博客很久沒有更新。 

    在上面一篇中已經實現了預讀微博的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 = "http://api.t.sina.com.cn/statuses/show/:id.json";
        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 = "http://api.t.sina.com.cn/statuses/counts.json";
        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);

    }

本篇到這裡就結束了,請繼續關注下一篇。  

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved