HTTP提交方式有多种,最常用的的就是POST和GET,另外还有PUT、DELETE、HEAD。好久没学习了,Android这边又生疏了,近三个月来毫无建树,整天都忙也没学到什么东西,打算继续学习Android!

1.0 HttpURLConnection

1.1 HttpURLConnection:GET

public boolean loginByGet(String path, String username , String password) throws Exception{                    String url_path = path +"?username=" + URLEncoder.encode(username, "utf-8") + "&password="+password;                    URL url = new URL(url_path);          HttpURLConnection conn = (HttpURLConnection) url.openConnection();                    conn.setRequestMethod("GET");          conn.setConnectTimeout(5000);          if(conn.getResponseCode() == 200){              return true;          }                              return false;      }  

1.2 HttpURLConnection:POST

public boolean loginByPost(String path,String username , String password) throws Exception{            System.out.println("LoginService的loginByPost()");          URL url = new URL(path);          HttpURLConnection conn = (HttpURLConnection) url.openConnection();                    conn.setRequestMethod("POST");          conn.setConnectTimeout(5000);                    String value = "username=" + username +"&" + "password=" +password;          byte[] entity = value.getBytes();                     conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");          conn.setRequestProperty("Content-Length", entity.length + "");                    conn.setDoOutput(true);          OutputStream os = conn.getOutputStream();          os.write(entity);                    if(conn.getResponseCode() == 200){              return true;          }                    return false;      }  

2.0 HttpClient

2.1 HttpClient:GET

public boolean loginByHttpClientGet(String path,String username , String password) throws Exception{                              String value = path + "?username=" + username +"&password=" + password;           HttpClient httpClient =  new DefaultHttpClient();          HttpGet httpGet = new HttpGet(value);                    HttpResponse httpResponse = httpClient.execute(httpGet);          if(httpResponse.getStatusLine().getStatusCode() == 200){              return true;          }                    return false;      }  

2.2 HttpClient:POST

public boolean loginByHttpClientPost(String path,String username , String password)throws Exception{                    HttpClient httpClient = new DefaultHttpClient();          HttpPost httpPost = new HttpPost(path);                    List<NameValuePair> parameters = new ArrayList<NameValuePair>();          parameters.add(new BasicNameValuePair("username", username));          parameters.add(new BasicNameValuePair("password", password));                    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,"utf-8");          httpPost.setEntity(entity);          HttpResponse httpResponse = httpClient.execute(httpPost);          if(httpResponse.getStatusLine().getStatusCode() == 200){              return true;          }                    return false;      }  

更多相关文章

  1. Android(安卓)UI学习 - Tab的学习和使用
  2. Android(安卓)Studio 学习笔记
  3. android学习之路 Handler用法
  4. 一步一步学习androidNDK编程(java给c传递数据)
  5. Appium学习第一个测试脚本
  6. Android(安卓)UI学习 - Tab的学习和使用
  7. 《第一行代码(第二版)》学习(二)
  8. Android(安卓)UI学习 - Tab的学习和使用
  9. Android开发秘籍学习笔记(一)

随机推荐

  1. 关于android Master 和 Stream 静音
  2. 自定义实现横向圆角进度条——简易版
  3. Android(安卓)学习笔记 Service (二) Remot
  4. Android解决TextView setText显示乱码
  5. Android-内存映射mmap
  6. [置顶] [Android(安卓)Studio 权威教程]Mac
  7. android 自定义View过程解析
  8. dex 转为 jar
  9. android eclipse 导入工程报错unable to
  10. Android(安卓)中Parcelable的作用 (转载)