標籤:android 伺服器 登陸
簡單 方便
/** * @author think *以同步方式發送Http請求 */public class ApacheHttpClient { /** * @return * */ public String httpGet(String uri) { String response=null;//響應 HttpClient httpClient=new DefaultHttpClient(); //建立HttpGet對象 HttpGet httpGet=new HttpGet(uri); HttpResponse httpResponse; try { //使用execute方法發送HTTP GET請求,並返回HttpResponse對象httpResponse=httpClient.execute(httpGet);int statusCode = httpResponse.getStatusLine().getStatusCode();//返回碼 ,if (statusCode==HttpStatus.SC_OK) {//獲得返回結果response=EntityUtils.toString(httpResponse.getEntity());}else {response = "返回碼:"+statusCode;}} catch (Exception e) {// TODO Auto-generated catch block e.printStackTrace();} System.out.println(response); return response;} /** * 以Post方式發送請求 * @param url 請求地址 * @param params 參數 ,Post方式必須用NameValuePair[]陣列儲存參數 * @return * @throws Exception */ public String httpPost(String uri,List<NameValuePair> params) throws Exception{ String response=null; HttpClient httpClient=new DefaultHttpClient(); HttpPost httpPost=new HttpPost(uri); try { //設定httpPost請求參數 httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8)); //使用execute方法發送HTTP Post請求,並返回HttpResponse對象 HttpResponse httpResponse=httpClient.execute(httpPost); int statusCode = httpResponse.getStatusLine().getStatusCode();//返回碼 , if (statusCode==HttpStatus.SC_OK) {response=EntityUtils.toString(httpResponse.getEntity());System.out.println("______________"+response);} else { response = "返回碼:"+statusCode; System.out.println("______________"+response);}} catch (Exception e) {e.printStackTrace();} return response;}}
android 伺服器操作類