mixer-a
BlogJava
::
首頁
::
新隨筆
::
聯系
::
聚合
::
管理
posts - 101, comments - 29, trackbacks - 0
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
給我留言
查看公開留言
查看私人留言
隨筆檔案
(101)
2012年8月 (2)
2012年7月 (13)
2012年6月 (6)
2012年5月 (6)
2012年4月 (26)
2012年3月 (16)
2012年2月 (17)
2012年1月 (15)
我的連接
a
iteye
csdn
www.javady.com
www.javady.com
搜索
最新評論
1.?re: android開發我的新浪微博客戶端-登錄頁面UI篇(4.1)[未登錄]
仿真時提示:No Launcher activity found!
--小龍
2.?re: android開發我的新浪微博客戶端-登錄頁面UI篇(4.1)[未登錄]
“新建名LoginActivity.java的Activity并且在AndroidManifest.xml中進行相應配置”請問怎么進行配置?因為新建的LoginActivity.java無法啟動仿真。
--小龍
3.?re: JS 時間處理和格式轉換
@Wanlic2008
不用把時間的年份拿出來單獨分析
--Wanlic2008
4.?re: JS 時間處理和格式轉換
評論內容較長,點擊標題查看
--Wanlic2008
5.?re: Android進階2之Http連接GET/POST請求
有用
--許大海
閱讀排行榜
1.?JS 時間處理和格式轉換(41185)
2.?Android進階2之Activity之間數據交流(onActivityResult的用法)(11501)
3.?[原]Android應用程序發送廣播(sendBroadcast)的過程分析(7302)
4.?[原]Android應用程序鍵盤(Keyboard)消息處理機制分析(4899)
5.?并行計算框架的Java實現--系列一(3785)
評論排行榜
1.?你想不到的壓縮方法:將javascript文件壓縮成PNG圖像存儲(4)
2.?MyEclipse快捷鍵設置(4)
3.?開發流程那些事:6天時間修改1行代碼(3)
4.?JS 時間處理和格式轉換(3)
5.?也談 GET 和 POST 的區別(2)
Android進階2之新浪微博采用Oauth發送圖片和文字
本以為新浪發布圖片和發布文字一樣的簡單,但很悲劇。。。。。。。遠不是想象的那樣。
現在分享一下代碼。
/** * 發表帶圖片的微博 * * @param token * @param tokenSecret * @param status * 發表的內容 * @param urlPath * 圖片的地址 本函數沒有用到,我直接在函數中創建的。 * @return */ public static void uploadStatus(String token, String tokenSecret, String imageUrl, String status) { File files = Environment.getExternalStorageDirectory(); String sdPath = files.getAbsolutePath(); // 請保證SD卡根目錄下有這張圖片文件 String urlPath = sdPath + "/" + "c.jpg"; File imageFile = new File(urlPath); if (!imageFile.exists()) { System.out.println("沒有該圖片"); } httpOauthConsumer = new DefaultOAuthConsumer("你的AppKey","你的AppSecret");//記得用你的替換一下 httpOauthConsumer.setTokenWithSecret(token, tokenSecret);//token tokenSecret之前工作的任務 try { URL url = new URL(Constants.XinLangUpload);//URL:http://api.t.sina.com.cn/statuses/upload.json HttpURLConnection request = (HttpURLConnection) url.openConnection(); request.setDoOutput(true); request.setRequestMethod("POST"); HttpParameters httpParameters = new HttpParameters(); httpParameters.put("status", URLEncoder.encode(status, "utf-8").replaceAll("\\+", "%20")); String boundary = "---------------------------37531613912423"; String content = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"status\"\r\n\r\n"; String pic = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"pic\"; filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n"; byte[] end_data = ("\r\n--" + boundary + "--\r\n").getBytes(); FileInputStream stream = new FileInputStream(imageFile); byte[] file = new byte[(int) imageFile.length()]; stream.read(file); request.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); // 設置表單類型和分隔符 request.setRequestProperty( "Content-Length", String.valueOf(content.getBytes().length + status.getBytes().length + pic.getBytes().length + imageFile.length() + end_data.length)); // 設置內容長度 httpOauthConsumer.setAdditionalParameters(httpParameters); httpOauthConsumer.sign(request); OutputStream outputStream = request.getOutputStream(); outputStream.write(content.getBytes()); outputStream.write(status.getBytes()); outputStream.write(pic.getBytes()); outputStream.write(file); outputStream.write(end_data); outputStream.flush(); outputStream.close(); request.connect(); if (200 == request.getResponseCode()) { System.out.println("發布成功"); } else { System.out.println("異常code:" + request.getResponseCode()); } } catch (Exception e1) { e1.printStackTrace(); } }
posted on 2012-04-06 16:26
mixer-a
閱讀(351)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
Powered by:
BlogJava
Copyright ©2025 mixer-a
主站蜘蛛池模板:
陇西县
|
天峻县
|
石柱
|
抚远县
|
SHOW
|
岑巩县
|
桂东县
|
十堰市
|
施甸县
|
青浦区
|
襄樊市
|
南岸区
|
临夏县
|
西乡县
|
忻州市
|
漠河县
|
延寿县
|
远安县
|
中牟县
|
荣昌县
|
东安县
|
嘉兴市
|
冀州市
|
平山县
|
永仁县
|
铅山县
|
凉城县
|
台东市
|
湖口县
|
拜城县
|
巴东县
|
临安市
|
泊头市
|
公安县
|
子洲县
|
定兴县
|
闽清县
|
荆门市
|
汨罗市
|
惠来县
|
黑水县
|