上一篇文章描述了webview获得accessToken方法。那么我们说一下分享。
分享有两个接口,他们是post要求
https://api.weibo.com/2/statuses/update.json//纯文本界面共享
https://upload.api.weibo.com/2/statuses/upload.json //文本图片分享接口
private static final String ADD_PIC_T = "https://upload.api.weibo.com/2/statuses/upload.json";private static final String ADD_T = "https://api.weibo.com/2/statuses/update.json"; private static final String[] ADD_SHARE_PARAMS = { "access_token", "status", "long", "lat" , "rip"};ApiInfo.java
public class ApiInfo { private static final String TAG = "ApiInfo"; public static final int REQ_GET = 0; public static final int REQ_POST = 1; public static final int REQ_PUT = 2; private String method; private String host; private int requstType; private String[] paramNames; private Map分享方法share2Weibo,拼接请求參数params; private String filePath; public ApiInfo(String host, String method, String[] paramNames) { this.host = host; this.method = method; this.paramNames = paramNames; this.params = new HashMap (); } public ApiInfo(String host, String method, String[] paramNames, int requestType) { this.host = host; this.method = method; this.paramNames = paramNames; this.requstType = requestType; this.params = new HashMap (); } public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } public int getRequstType() { return requstType; } public void setRequstType(int requstType) { this.requstType = requstType; } public String getMethod() { return method; } public void setMethod(String method) { this.method = method; } public String[] getParamNames() { return paramNames; } public void setParamNames(String[] paramNames) { this.paramNames = paramNames; } public Map getParams() { if (params == null) params = new HashMap (); return params; } public void setParams(Map params) { this.params = params; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getPlainUrl() { StringBuffer url = new StringBuffer(); url.append(host); url.append(method); url.append("?"); for (String paramName : getParamNames()) { LogInfo.d(TAG, "paramName=" + paramName); url.append(paramName); url.append("="); if (getParams().get(paramName) != null) try { url.append(URLEncoder.encode(getParams().get(paramName).toString(), "UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } url.append("&"); } url.deleteCharAt(url.length() - 1); return url.toString(); }}
public void share2Weibo(String content, String img) { ApiInfo apiInfo = new ApiInfo(ADD_PIC_T, "", ADD_PIC_T_PARAMS, ApiInfo.REQ_POST); Mapparams = new HashMap (); params.put(ADD_PIC_T_PARAMS[0], oAuth.accessToken); params.put(ADD_PIC_T_PARAMS[1], content); params.put(ADD_PIC_T_PARAMS[2], "0.0"); params.put(ADD_PIC_T_PARAMS[3], "0.0"); params.put(ADD_PIC_T_PARAMS[4], AndroidUtils.GetHostIp()); apiInfo.setParams(params); String imgPath = null; if (!TextUtils.isEmpty(img)) if (IOUtils.getCacheImageFile(context, img) != null) {//通过path推断图片是否存在 imgPath = IOUtils.getCacheImageFile(context, img).getAbsolutePath(); } else apiInfo.setHost(ADD_T);//假设图片不存在更改分享内容的url new ShareTask(apiInfo).execute(imgPath);}
ShareTask.java 联网发送post请求,分享内容
class ShareTask extends AsyncTask{ private ApiInfo apiInfo; public ShareTask(ApiInfo apiInfo) { this.apiInfo = apiInfo; } @Override protected Json doInBackground(String... params) { String result = ""; if (params.length > 0 && params[0] != null) { result = new String(httpClient.uploadFile(apiInfo.getPlainUrl(), "pic", params[0])); } else result = new String(httpClient.request(apiInfo.getPlainUrl(), apiInfo.getRequstType() == ApiInfo.REQ_POST)); LogInfo.d(TAG, "result:" + result); Json json = new Json(result); return json; } @Override protected void onPostExecute(Json result) { super.onPostExecute(result); }}
版权声明:本文博客原创文章,博客,未经同意,不得转载。