Java tutorial
/* * Copyright (c) 2013 Wobang Network. * * Licensed under the GNU General Public License, version 2 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.gnu.org/licenses/gpl-2.0.html * * Unless required by applicable law or agreed to in writing, software distributed under the License * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing permissions and limitations under * the License. */ package com.ihelpoo.app.api; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; import com.ihelpoo.app.AppContext; import com.ihelpoo.app.AppException; import com.ihelpoo.app.bean.AcademyList; import com.ihelpoo.app.bean.DormList; import com.ihelpoo.app.bean.MajorList; import com.ihelpoo.app.bean.MobileCodeResult; import com.ihelpoo.app.bean.MobileRegisterResult; import com.ihelpoo.app.bean.SchoolList; import com.ihelpoo.app.bean.WordList; import com.ihelpoo.app.bean.Blog; import com.ihelpoo.app.bean.BlogCommentList; import com.ihelpoo.app.bean.BlogList; import com.ihelpoo.app.bean.CommentList; import com.ihelpoo.app.bean.FavoriteList; import com.ihelpoo.app.bean.FriendList; import com.ihelpoo.app.bean.MessageList; import com.ihelpoo.app.bean.MyInformation; import com.ihelpoo.app.bean.News; import com.ihelpoo.app.bean.NewsList; import com.ihelpoo.app.bean.Notice; import com.ihelpoo.app.bean.Post; import com.ihelpoo.app.bean.PostList; import com.ihelpoo.app.bean.Result; import com.ihelpoo.app.bean.SearchList; import com.ihelpoo.app.bean.Software; import com.ihelpoo.app.bean.SoftwareCatalogList; import com.ihelpoo.app.bean.SoftwareList; import com.ihelpoo.app.bean.Tweet; import com.ihelpoo.app.bean.TweetList; import com.ihelpoo.app.bean.URLs; import com.ihelpoo.app.bean.Update; import com.ihelpoo.app.bean.User; import com.ihelpoo.app.bean.UserInformation; import com.ihelpoo.app.common.DeviceUtil; import com.ihelpoo.app.common.StringUtils; import com.ihelpoo.app.ui.NavWelcome; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.multipart.FilePart; import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; import org.apache.commons.httpclient.methods.multipart.Part; import org.apache.commons.httpclient.methods.multipart.StringPart; import org.apache.commons.httpclient.params.HttpMethodParams; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; /** * API?? * * @version 1.0 * @created 2012-3-21 */ public class ApiClient { public static final String UTF_8 = "UTF-8"; public static final String DESC = "descend"; public static final String ASC = "ascend"; private final static int TIMEOUT_CONNECTION = 20000; private final static int TIMEOUT_SOCKET = 20000; private final static int RETRY_TIME = 3; private static String appCookie; private static String appUserAgent; public static void cleanCookie() { appCookie = ""; } private static String getCookie(AppContext appContext) { if (appCookie == null || appCookie == "") { appCookie = appContext.getProperty("cookie"); } return appCookie; } private static String getUserAgent(AppContext appContext) { if (appUserAgent == null || appUserAgent == "") { StringBuilder ua = new StringBuilder("ihelpoo.com"); ua.append( '/' + appContext.getPackageInfo().versionName + '_' + appContext.getPackageInfo().versionCode);//App ua.append("/Android");//? ua.append("/" + android.os.Build.VERSION.RELEASE);// ua.append("/" + android.os.Build.MODEL); //? ua.append("/" + appContext.getAppId());// appUserAgent = ua.toString(); } return appUserAgent; } private static HttpClient getHttpClient() { HttpClient httpClient = new HttpClient(); // HttpClient Cookie,? httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); // ?? httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); // httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(TIMEOUT_CONNECTION); // ? httpClient.getHttpConnectionManager().getParams().setSoTimeout(TIMEOUT_SOCKET); // httpClient.getParams().setContentCharset(UTF_8); return httpClient; } private static GetMethod getHttpGet(String url, String cookie, String userAgent) { GetMethod httpGet = new GetMethod(url); // httpGet.getParams().setSoTimeout(TIMEOUT_SOCKET); httpGet.setRequestHeader("Host", URLs.HOST); httpGet.setRequestHeader("Connection", "Keep-Alive"); httpGet.setRequestHeader("Cookie", cookie); httpGet.setRequestHeader("User-Agent", userAgent); return httpGet; } private static PostMethod getHttpPost(String url, String cookie, String userAgent) { PostMethod httpPost = new PostMethod(url); // httpPost.getParams().setSoTimeout(TIMEOUT_SOCKET); httpPost.setRequestHeader("Host", URLs.HOST); httpPost.setRequestHeader("Connection", "Keep-Alive"); httpPost.setRequestHeader("Cookie", cookie); httpPost.setRequestHeader("User-Agent", userAgent); return httpPost; } private static String _MakeURL(String p_url, Map<String, Object> params) { StringBuilder url = new StringBuilder(p_url); if (url.indexOf("?") < 0) url.append('?'); for (String name : params.keySet()) { url.append('&'); url.append(name); url.append('='); url.append(String.valueOf(params.get(name))); //??URLEncoder? //url.append(URLEncoder.encode(String.valueOf(params.get(name)), UTF_8)); } return url.toString().replace("?&", "?"); } /** * getURL * * @param url * @throws com.ihelpoo.app.AppException */ private static InputStream http_get(AppContext appContext, String url) throws AppException { System.out.println("get_url==> " + url); String cookie = getCookie(appContext); String userAgent = getUserAgent(appContext); HttpClient httpClient = null; GetMethod httpGet = null; String responseBody = ""; int time = 0; do { try { httpClient = getHttpClient(); httpGet = getHttpGet(url, cookie, userAgent); int statusCode = httpClient.executeMethod(httpGet); if (statusCode != HttpStatus.SC_OK) { throw AppException.http(statusCode); } responseBody = httpGet.getResponseBodyAsString(); // System.out.println("XMLDATA=====>"+responseBody); break; } catch (HttpException e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(1000); } catch (InterruptedException e1) { } continue; } // ????? e.printStackTrace(); throw AppException.http(e); } catch (IOException e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(1000); } catch (InterruptedException e1) { } continue; } // ? e.printStackTrace(); throw AppException.network(e); } finally { // httpGet.releaseConnection(); httpClient = null; } } while (time < RETRY_TIME); responseBody = responseBody.replaceAll("\\p{Cntrl}", ""); // if (responseBody.contains("result") && responseBody.contains("errorCode") && appContext.containsProperty("user.uid")) {//FIXME // try { // Result res = Result.parse(new ByteArrayInputStream(responseBody.getBytes())); // if (res.getErrorCode() == 0) { // appContext.logout(); // appContext.getUnLoginHandler().sendEmptyMessage(1); // } // } catch (Exception e) { // e.printStackTrace(); // } // } return new ByteArrayInputStream(responseBody.getBytes()); } /** * post * * @param url * @param params * @param files * @throws AppException */ private static InputStream _post(AppContext appContext, String url, Map<String, Object> params, Map<String, File> files) throws AppException { //System.out.println("post_url==> "+url); String cookie = getCookie(appContext); String userAgent = getUserAgent(appContext); HttpClient httpClient = null; PostMethod httpPost = null; //post??? int length = (params == null ? 0 : params.size()) + (files == null ? 0 : files.size()); Part[] parts = new Part[length]; int i = 0; if (params != null) for (String name : params.keySet()) { parts[i++] = new StringPart(name, String.valueOf(params.get(name)), UTF_8); } if (files != null) for (String file : files.keySet()) { try { parts[i++] = new FilePart(file, files.get(file)); } catch (FileNotFoundException e) { e.printStackTrace(); } //System.out.println("post_key_file==> "+file); } String responseBody = ""; int time = 0; do { try { httpClient = getHttpClient(); httpPost = getHttpPost(url, cookie, userAgent); httpPost.setRequestEntity(new MultipartRequestEntity(parts, httpPost.getParams())); int statusCode = httpClient.executeMethod(httpPost); if (statusCode != HttpStatus.SC_OK) { throw AppException.http(statusCode); } else if (statusCode == HttpStatus.SC_OK) { Cookie[] cookies = httpClient.getState().getCookies(); String tmpcookies = ""; for (Cookie ck : cookies) { tmpcookies += ck.toString() + ";"; } //?cookie if (appContext != null && tmpcookies != "") { appContext.setProperty("cookie", tmpcookies); appCookie = tmpcookies; } } responseBody = httpPost.getResponseBodyAsString(); // System.out.println("XMLDATA=====>"+responseBody); break; } catch (HttpException e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(1000); } catch (InterruptedException e1) { } continue; } // ????? e.printStackTrace(); throw AppException.http(e); } catch (IOException e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(1000); } catch (InterruptedException e1) { } continue; } // ? e.printStackTrace(); throw AppException.network(e); } finally { // httpPost.releaseConnection(); httpClient = null; } } while (time < RETRY_TIME); responseBody = responseBody.replaceAll("\\p{Cntrl}", ""); // if(responseBody.contains("result") && responseBody.contains("errorCode") && appContext.containsProperty("user.uid")){//FIXME // try { // Result res = Result.parse(new ByteArrayInputStream(responseBody.getBytes())); // if(res.getErrorCode() == 0){ // appContext.logout(); // appContext.getUnLoginHandler().sendEmptyMessage(1); // } // } catch (Exception e) { // e.printStackTrace(); // } // } return new ByteArrayInputStream(responseBody.getBytes()); } /** * postURL * * @param url * @param params * @param files * @throws AppException * @throws IOException * @throws */ private static Result http_post(AppContext appContext, String url, Map<String, Object> params, Map<String, File> files) throws AppException, IOException { return Result.parse(_post(appContext, url, params, files)); } /** * ? * * @param url * @return */ public static Bitmap getNetBitmap(String url) throws AppException { //System.out.println("image_url==> "+url); HttpClient httpClient = null; GetMethod httpGet = null; Bitmap bitmap = null; int time = 0; do { try { httpClient = getHttpClient(); httpGet = getHttpGet(url, null, null); int statusCode = httpClient.executeMethod(httpGet); if (statusCode != HttpStatus.SC_OK) { throw AppException.http(statusCode); } InputStream inStream = httpGet.getResponseBodyAsStream(); bitmap = BitmapFactory.decodeStream(inStream); inStream.close(); break; } catch (HttpException e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(1000); } catch (InterruptedException e1) { } continue; } // ????? e.printStackTrace(); throw AppException.http(e); } catch (IOException e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(1000); } catch (InterruptedException e1) { } continue; } // ? e.printStackTrace(); throw AppException.network(e); } finally { // httpGet.releaseConnection(); httpClient = null; } } while (time < RETRY_TIME); return bitmap; } /** * * * @param appContext * @return */ public static Update checkVersion(AppContext appContext) throws AppException { try { return Update.parse(http_get(appContext, URLs.UPDATE_VERSION)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ?cookie * * @param appContext * @param username * @param pwd * @return * @throws AppException */ public static User login(AppContext appContext, String username, String pwd, String status) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("username", username); params.put("pwd", pwd); params.put("status", status); params.put("ip", DeviceUtil.getIPAddress(true)); String loginurl = URLs.LOGIN_VALIDATE_HTTP; if (appContext.isHttpsLogin()) { loginurl = URLs.LOGIN_VALIDATE_HTTPS; } try { return User.parse(_post(appContext, loginurl, params, null)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * * * @param appContext * @param uid * @return * @throws AppException */ public static MyInformation myInformation(AppContext appContext, int uid) throws AppException { // Map<String,Object> params = new HashMap<String,Object>(); // params.put("uid", uid); String newUrl = _MakeURL(URLs.MY_INFORMATION.replace("${uid}", String.valueOf(uid)), new HashMap<String, Object>() { { } }); try { return MyInformation.parse(http_get(appContext, newUrl)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ? * * @param appContext * @param uid ?uid * @param portrait ? * @return * @throws AppException */ public static Result updatePortrait(AppContext appContext, int uid, File portrait) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("uid", uid); Map<String, File> files = new HashMap<String, File>(); files.put("portrait", portrait); try { return http_post(appContext, URLs.PORTRAIT_UPDATE, params, files); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ??????? * * @param hisUid * @param hisName @return * @param uid uid * @param pageIndex ? * @param pageSize ??? * @throws AppException */ public static UserInformation information(AppContext appContext, int hisUid, String hisName, int uid, int pageIndex, int pageSize) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("his_uid", hisUid); params.put("his_name", hisName); params.put("uid", uid); params.put("page_index", pageIndex); params.put("page_size", pageSize); try { return UserInformation.parse(_post(appContext, URLs.USER_INFORMATION, params, null)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ?? * * @param uid uid * @param hisuid uid * @param newrelation 0:? 1: * @return * @throws AppException */ public static Result updateRelation(AppContext appContext, int uid, int hisuid, int newrelation) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("uid", uid); params.put("his_uid", hisuid); params.put("relation", newrelation); try { return Result.parse(_post(appContext, URLs.USER_UPDATERELATION, params, null)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ?? * * @param uid * @return * @throws AppException */ public static Notice getUserNotice(AppContext appContext, int uid) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("uid", uid); try { return Notice.parse(_post(appContext, URLs.USER_NOTICE, params, null)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ? * * @param uid * @param type 1:@? 2:? 3: 4:? * @param fromUid * @return * @throws AppException */ public static Result noticeClear(AppContext appContext, int uid, int type, int fromUid) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("uid", uid); params.put("type", type); params.put("from_uid", fromUid); try { return Result.parse(_post(appContext, URLs.NOTICE_CLEAR, params, null)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ?? * * @param uid * @param relation 0:? 1: * @param pageIndex * @param pageSize * @return * @throws AppException */ public static FriendList getFriendList(AppContext appContext, final int uid, final int relation, final int pageIndex, final int pageSize) throws AppException { String newUrl = _MakeURL(URLs.FRIENDS_LIST, new HashMap<String, Object>() { { put("uid", uid); put("relation", relation); put("page_index", pageIndex); put("page_size", pageSize); } }); try { return FriendList.parse(http_get(appContext, newUrl)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ? * * @param url * @param catalog * @param pageIndex * @param pageSize * @return * @throws AppException */ public static NewsList getNewsList(AppContext appContext, final int uid, final int catalog, final int pageIndex, final int pageSize) throws AppException { String newUrl = _MakeURL(URLs.NEST_LIST, new HashMap<String, Object>() { { put("uid", uid); put("catalog", catalog); put("pageIndex", pageIndex); put("pageSize", pageSize); } }); try { return NewsList.parse(http_get(appContext, newUrl)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } public static NewsList getNoticeList(AppContext appContext, final int loginUid, final int catalog, final int pageIndex, final int pageSize) throws AppException { String newUrl = _MakeURL(URLs.NOTICE_LIST, new HashMap<String, Object>() { { put("uid", loginUid); put("catalog", catalog); put("pageIndex", pageIndex); put("pageSize", pageSize); } }); try { return NewsList.parse(http_get(appContext, newUrl)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ? * * @param url * @param news_id * @return * @throws AppException */ public static News getNewsDetail(AppContext appContext, final int news_id) throws AppException { String newUrl = _MakeURL(URLs.NEWS_DETAIL, new HashMap<String, Object>() { { put("id", news_id); } }); try { return News.parse(http_get(appContext, newUrl)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ??? * * @param authoruid * @param uid * @param pageIndex * @param pageSize * @return * @throws AppException */ public static BlogList getUserBlogList(AppContext appContext, final int authoruid, final String authorname, final int uid, final int pageIndex, final int pageSize) throws AppException { String newUrl = _MakeURL(URLs.USERBLOG_LIST, new HashMap<String, Object>() { { // put("authoruid", authoruid); // put("authorname", URLEncoder.encode(authorname)); // put("uid", uid); // put("pageIndex", pageIndex); // put("pageSize", pageSize); put("authoruid", 12); put("authorname", URLEncoder.encode("")); put("uid", 915579); put("pageIndex", 1); put("pageSize", 30); } }); try { return BlogList.parse(http_get(appContext, newUrl)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ?? * * @param type ??recommend latest * @param pageIndex * @param pageSize * @return * @throws AppException */ public static BlogList getBlogList(AppContext appContext, final String type, final int pageIndex, final int pageSize) throws AppException { String newUrl = _MakeURL(URLs.BLOG_LIST, new HashMap<String, Object>() { { put("type", type); put("pageIndex", pageIndex); put("pageSize", pageSize); } }); try { return BlogList.parse(http_get(appContext, newUrl)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ?? * * @param uid * @param authoruid * @param id * @return * @throws AppException */ public static Result delBlog(AppContext appContext, int uid, int authoruid, int id) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("uid", uid); params.put("authoruid", authoruid); params.put("id", id); try { return http_post(appContext, URLs.USERBLOG_DELETE, params, null); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ?? * * @param blog_id * @return * @throws AppException */ public static Blog getBlogDetail(AppContext appContext, final int blog_id) throws AppException { String newUrl = _MakeURL(URLs.BLOG_DETAIL, new HashMap<String, Object>() { { put("id", blog_id); } }); try { return Blog.parse(http_get(appContext, newUrl)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ?? * * @param schoolId * @param catalog * @param pageIndex * @return * @throws AppException */ public static PostList getPostList(AppContext appContext, final String schoolId, final int catalog, final int pageIndex, final int pageSize) throws AppException { String newUrl = _MakeURL(URLs.POST_LIST, new HashMap<String, Object>() { { put("school_id", schoolId); put("catalog", catalog); put("page_index", pageIndex); put("page_size", pageSize); } }); try { return PostList.parse(http_get(appContext, newUrl)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * Tag?? * * @param url * @param catalog * @param pageIndex * @return * @throws AppException */ public static PostList getPostListByTag(AppContext appContext, final String tag, final int pageIndex, final int pageSize) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("tag", tag); params.put("pageIndex", pageIndex); params.put("pageSize", pageSize); try { return PostList.parse(_post(appContext, URLs.POST_LIST, params, null)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ?? * * @param url * @param post_id * @return * @throws AppException */ public static Post getPostDetail(AppContext appContext, final int post_id) throws AppException { String newUrl = _MakeURL(URLs.POST_DETAIL, new HashMap<String, Object>() { { put("id", post_id); } }); try { return Post.parse(http_get(appContext, newUrl)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ?? * * @param post uid?title?catalog?content?isNoticeMe * @return * @throws AppException */ public static Result pubPost(AppContext appContext, Post post) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("uid", post.getAuthorId()); params.put("title", post.getTitle()); params.put("catalog", post.getCatalog()); params.put("content", post.getBody()); params.put("isNoticeMe", post.getIsNoticeMe()); try { return http_post(appContext, URLs.POST_PUB, params, null); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ? * * @param catalog * @param uid * @param schoolId * @param pageIndex * @param pageSize * @return * @throws AppException */ public static TweetList getTweetList(AppContext appContext, final int catalog, final int uid, String schoolId, final int pageIndex, final int pageSize) throws AppException { String school = "35"; final User user = appContext.getLoginInfo(); if (!StringUtils.isEmpty(schoolId) && !school.equals(schoolId)) { school = schoolId; } else { school = String.valueOf(user.getSchool_id()); } final String finalSchool = school; String newUrl = _MakeURL(URLs.TWEET_LIST, new HashMap<String, Object>() { { put("catalog", catalog); put("uid", uid); put("schoolId", finalSchool); put("pageIndex", pageIndex); put("pageSize", pageSize); } }); try { return TweetList.parse(http_get(appContext, newUrl)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ? * * @param tweet_id * @param uid * @return * @throws AppException */ public static Tweet getTweetDetail(AppContext appContext, final int tweet_id, final int uid) throws AppException { String newUrl = _MakeURL(URLs.TWEET_DETAIL.replace("${id}", String.valueOf(tweet_id)), new HashMap<String, Object>() { { put("sid", tweet_id); put("uid", uid); } }); try { return Tweet.parse(http_get(appContext, newUrl)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ? * * @param Tweet-uid & msg & image * @return * @throws AppException */ public static Result pubTweet(AppContext appContext, Tweet tweet) throws AppException { SharedPreferences preferences = appContext.getSharedPreferences(NavWelcome.GLOBAL_CONFIG, AppContext.MODE_PRIVATE); int targetSchool = preferences.getInt(NavWelcome.CHOOSE_SCHOOL, NavWelcome.DEFAULT_SCHOOL); Map<String, Object> params = new HashMap<String, Object>(); params.put("uid", tweet.getAuthorId()); params.put("msg", tweet.getBody()); params.put("reward", tweet.getReward()); params.put("target_school", targetSchool); Map<String, File> files = new HashMap<String, File>(); if (tweet.getImageFile() != null) files.put("img", tweet.getImageFile()); try { return http_post(appContext, URLs.TWEET_PUB, params, files); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * * * @param uid * @param tweetid * @return * @throws AppException */ public static Result delTweet(AppContext appContext, int uid, int tweetid) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("uid", uid); params.put("sid", tweetid); try { return http_post(appContext, URLs.TWEET_DELETE, params, null); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ?? * * @param uid * @param catalog 1 2@ 3 * @param pageIndex * @param pageSize * @return * @throws AppException */ public static WordList getWordList(AppContext appContext, final int uid, final int catalog, final int pageIndex, final int pageSize) throws AppException { final User user = appContext.getLoginInfo(); String newUrl = _MakeURL(URLs.WORD_LIST, new HashMap<String, Object>() { { put("uid", uid); put("catalog", catalog); put("schoolId", user.getSchool_id()); put("pageIndex", pageIndex); put("pageSize", pageSize); } }); try { return WordList.parse(http_get(appContext, newUrl)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ? * * @param uid * @param pageIndex * @return * @throws AppException */ public static MessageList getMessageList(AppContext appContext, final int uid, final int pageIndex, final int pageSize) throws AppException { String newUrl = _MakeURL(URLs.MESSAGE_LIST, new HashMap<String, Object>() { { put("uid", uid); put("pageIndex", pageIndex); put("pageSize", pageSize); } }); try { return MessageList.parse(http_get(appContext, newUrl)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ?? * * @param uid uid * @param receiver ?id * @param content ???250 * @return * @throws AppException */ public static Result pubMessage(AppContext appContext, int uid, int receiver, String content) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("uid", uid); params.put("receiver", receiver); params.put("content", content); try { return http_post(appContext, URLs.MESSAGE_PUB, params, null); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ? * * @param uid uid * @param receiver ??? * @param content ???250 * @return * @throws AppException */ public static Result forwardMessage(AppContext appContext, int uid, String receiverName, String content) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("uid", uid); params.put("receiver_nickname", receiverName); params.put("content", content); try { return http_post(appContext, URLs.MESSAGE_PUB, params, null); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * * * @param uid uid * @param friendid id * @return * @throws AppException */ public static Result delMessage(AppContext appContext, int uid, int friendid) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("uid", uid); params.put("friend_id", friendid); try { return http_post(appContext, URLs.MESSAGE_DELETE, params, null); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ?? * * @param id ?id * @param pageIndex * @param pageSize * @return * @throws AppException */ public static BlogCommentList getBlogCommentList(AppContext appContext, final int id, final int pageIndex, final int pageSize) throws AppException { String newUrl = _MakeURL(URLs.BLOGCOMMENT_LIST, new HashMap<String, Object>() { { put("id", id); put("pageIndex", pageIndex); put("pageSize", pageSize); } }); try { return BlogCommentList.parse(http_get(appContext, newUrl)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ?? * * @param blog ?id * @param uid uid * @param content * @return * @throws AppException */ public static Result pubBlogComment(AppContext appContext, int blog, int uid, String content) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("blog", blog); params.put("uid", uid); params.put("content", content); try { return http_post(appContext, URLs.BLOGCOMMENT_PUB, params, null); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ?? * * @param blog ?id * @param uid uid * @param content * @param reply_id id * @param objuid ?uid * @return * @throws AppException */ public static Result replyBlogComment(AppContext appContext, int blog, int uid, String content, int reply_id, int objuid) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("blog", blog); params.put("uid", uid); params.put("content", content); params.put("reply_id", reply_id); params.put("objuid", objuid); try { return http_post(appContext, URLs.BLOGCOMMENT_PUB, params, null); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ? * * @param uid uid * @param blogid ?id * @param replyid id * @param authorid ?uid * @param owneruid ?uid * @return * @throws AppException */ public static Result delBlogComment(AppContext appContext, int uid, int blogid, int replyid, int authorid, int owneruid) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("uid", uid); params.put("blogid", blogid); params.put("replyid", replyid); params.put("authorid", authorid); params.put("owneruid", owneruid); try { return http_post(appContext, URLs.BLOGCOMMENT_DELETE, params, null); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ? * * * @param isHelp * @param catalog 1 2? 3 4? * @param uid * @param id * @param pageIndex * @param pageSize @return * @throws AppException */ public static CommentList getCommentList(AppContext appContext, final boolean isHelp, final int catalog, final int uid, final int id, final int pageIndex, final int pageSize) throws AppException { String newUrl = _MakeURL(URLs.COMMENT_LIST, new HashMap<String, Object>() { { put("is_help", isHelp); put("catalog", catalog); put("uid", uid); put("sid", id); put("page_index", pageIndex); put("page_size", pageSize); } }); try { return CommentList.parse(http_get(appContext, newUrl)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ? * * * @param catalog 1 2? 3 4? * @param id ???id * @param uid uid * @param content ? * @param isHelp ?? 0?? 1? * @return * @throws AppException */ public static Result pubComment(AppContext appContext, int catalog, int id, int uid, String content, Boolean isHelp) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("catalog", catalog); params.put("id", id); params.put("uid", uid); params.put("content", content); params.put("is_help", isHelp); try { return http_post(appContext, URLs.COMMENT_PUB, params, null); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } public static Result plus(AppContext appContext, int id, int uid) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("sid", id); params.put("uid", uid); try { return http_post(appContext, URLs.PLUS, params, null); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } public static Result diffuse(AppContext appContext, int id, int uid, String content) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("sid", id); params.put("uid", uid); params.put("content", content); try { return http_post(appContext, URLs.DIFFUSE, params, null); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * * * @param id ???id ??? friendid * @param catalog 1 2? 3 4? * @param replyid ??id * @param authorid id * @param author *@param uid uid ?uid * @param content ? * @param isHelp @return * @throws AppException */ public static Result replyComment(AppContext appContext, int id, int catalog, int replyid, int authorid, String author, int uid, String content, boolean isHelp) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("catalog", catalog); params.put("id", id); params.put("uid", uid); params.put("content", content); params.put("replyid", replyid); params.put("authorid", authorid); params.put("author", author); params.put("is_help", isHelp); try { return http_post(appContext, URLs.COMMENT_REPLY, params, null); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * * * * @param id ??,?,id ??? friendid * @param catalog 1 2? 3 4?& * @param replyid ??id * @param authorid id * @param isHelp * @return * @throws AppException */ public static Result delComment(AppContext appContext, int id, int catalog, int replyid, int authorid, Boolean isHelp) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("id", id); params.put("catalog", catalog); params.put("replyid", replyid); params.put("authorid", authorid); params.put("is_help", isHelp); try { return http_post(appContext, URLs.COMMENT_DELETE, params, null); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ? * * @param uid UID * @param type 0:? 1: 2:? 3:? 4: 5:? * @param pageIndex ? 0 * @param pageSize ?? * @return * @throws AppException */ public static FavoriteList getFavoriteList(AppContext appContext, final int uid, final int type, final int pageIndex, final int pageSize) throws AppException { String newUrl = _MakeURL(URLs.FAVORITE_LIST, new HashMap<String, Object>() { { put("uid", uid); put("type", type); put("pageIndex", pageIndex); put("pageSize", pageSize); } }); try { return FavoriteList.parse(http_get(appContext, newUrl)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ? * * @param uid UID * @param objid ID ID ID * @param type 1: 2:? 3:? 4: 5:? * @return * @throws AppException */ public static Result addFavorite(AppContext appContext, int uid, int objid, int type) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("uid", uid); params.put("objid", objid); params.put("type", type); try { return http_post(appContext, URLs.FAVORITE_ADD, params, null); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ? * * @param uid UID * @param objid ID ID ID * @param type 1: 2:? 3:? 4: 5:? * @return * @throws AppException */ public static Result delFavorite(AppContext appContext, int uid, int objid, int type) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("uid", uid); params.put("objid", objid); params.put("type", type); try { return http_post(appContext, URLs.FAVORITE_DELETE, params, null); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ?? * * @param catalog :all :news :post :software ?:blog ?:code * @param content ? * @param pageIndex * @param pageSize * @return * @throws AppException */ public static SearchList getSearchList(AppContext appContext, String catalog, String content, int pageIndex, int pageSize) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("catalog", catalog); params.put("content", content); params.put("pageIndex", pageIndex); params.put("pageSize", pageSize); try { return SearchList.parse(_post(appContext, URLs.SEARCH_LIST, params, null)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * * * @param searchTag ??:recommend :time :view :list_cn * @param pageIndex * @param pageSize * @return * @throws AppException */ public static SoftwareList getSoftwareList(AppContext appContext, final String searchTag, final int pageIndex, final int pageSize) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("searchTag", searchTag); params.put("pageIndex", pageIndex); params.put("pageSize", pageSize); try { return SoftwareList.parse(_post(appContext, URLs.SOFTWARE_LIST, params, null)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * * * @param searchTag softwarecatalog_list?tag * @param pageIndex * @param pageSize * @return * @throws AppException */ public static SoftwareList getSoftwareTagList(AppContext appContext, final int searchTag, final int pageIndex, final int pageSize) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("searchTag", searchTag); params.put("pageIndex", pageIndex); params.put("pageSize", pageSize); try { return SoftwareList.parse(_post(appContext, URLs.SOFTWARETAG_LIST, params, null)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * * * @param tag :0 :tag * @return * @throws AppException */ public static SoftwareCatalogList getSoftwareCatalogList(AppContext appContext, final int tag) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("tag", tag); try { return SoftwareCatalogList.parse(_post(appContext, URLs.SOFTWARECATALOG_LIST, params, null)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } /** * ? * * @param ident * @return * @throws AppException */ public static Software getSoftwareDetail(AppContext appContext, final String ident) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("ident", ident); try { return Software.parse(_post(appContext, URLs.SOFTWARE_DETAIL, params, null)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } public static MobileRegisterResult thirdLogin(AppContext appContext, String thirdUid, String thirdType, Integer schoolId, String nickname, String status) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("third_uid", thirdUid); params.put("third_type", thirdType); params.put("school_id", schoolId); params.put("ip", DeviceUtil.getIPAddress(true)); params.put("nickname", nickname); params.put("status", status); try { return MobileRegisterResult.parse(_post(appContext, URLs.THIRD_LOGIN, params, null)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } public static MobileRegisterResult mobileRegister(AppContext appContext, String mobileCode, String mobileNo, String pwd, String schoolId) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("code", mobileCode); params.put("mobile", mobileNo); params.put("pwd", pwd); params.put("schoolId", schoolId); params.put("ip", DeviceUtil.getIPAddress(true)); try { return MobileRegisterResult.parse(_post(appContext, URLs.MOBILE_REGISTER, params, null)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } public static MobileCodeResult mobileCode(AppContext appContext, String mobileNo) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("mobile", mobileNo); try { return MobileCodeResult.parse(_post(appContext, URLs.MOBILE_CODE, params, null)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } public static SchoolList getSchoolList(AppContext appContext) throws AppException { try { return SchoolList.parse(http_get(appContext, URLs.SCHOOL_LIST)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } public static Result updateNickname(AppContext appContext, int uid, String nickname) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("uid", uid); params.put("nickname", nickname); try { return Result.parse(_post(appContext, URLs.USER_UPDATE_NICKNAME, params, null)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } public static Result updateGender(AppContext appContext, int uid, String gender) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("uid", uid); params.put("gender", gender); try { return Result.parse(_post(appContext, URLs.USER_UPDATE_GENDER, params, null)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } public static Result updateEnrol(AppContext appContext, int uid, String newEnrol) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("uid", uid); params.put("enrol", newEnrol); try { return Result.parse(_post(appContext, URLs.USER_UPDATE_ENROL, params, null)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } public static Result updateIntro(AppContext appContext, int uid, String intro) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("uid", uid); params.put("intro", intro); try { return Result.parse(_post(appContext, URLs.USER_UPDATE_INTRO, params, null)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } public static AcademyList getAcademyList(AppContext appContext, final int schoolId) throws AppException { String newUrl = _MakeURL(URLs.ACADEMY_LIST, new HashMap<String, Object>() { { put("school_id", schoolId); } }); try { return AcademyList.parse(http_get(appContext, newUrl)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } public static MajorList getMajorList(AppContext appContext, final int schoolId, final Integer academyId) throws AppException { String newUrl = _MakeURL(URLs.MAJOR_LIST, new HashMap<String, Object>() { { put("school_id", schoolId); put("academy_id", academyId); } }); try { return MajorList.parse(http_get(appContext, newUrl)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } public static DormList getDormList(AppContext appContext, final int schoolId) throws AppException { String newUrl = _MakeURL(URLs.DORM_LIST, new HashMap<String, Object>() { { put("school_id", schoolId); } }); try { return DormList.parse(http_get(appContext, newUrl)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } public static Result updateMajor(AppContext appContext, int uid, String schoolId, String academyId, String majorId, String dormId) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("uid", uid); params.put("school_id", schoolId); params.put("academy_id", academyId); params.put("major_id", majorId); params.put("dorm_id", dormId); try { return Result.parse(_post(appContext, URLs.USER_UPDATE_MAJOR, params, null)); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } } }