现在的位置: 首页 > 综合 > 正文

android发送get请求

2018年04月19日 ⁄ 综合 ⁄ 共 693字 ⁄ 字号 评论关闭

一、权限

<uses-permission android:name="android.permission.INTERNET"/>

二、代码

public static boolean sendGetRequest(String path, Map<String, String> params, String enc) throws Exception{
  StringBuilder sb = new StringBuilder(path);
  sb.append('?');
  // ?method=save&title=435435435&timelength=89&
  for(Map.Entry<String, String> entry : params.entrySet()){
   sb.append(entry.getKey()).append('=')
    .append(URLEncoder.encode(entry.getValue(), enc)).append('&');
  }
  sb.deleteCharAt(sb.length()-1);
  
  URL url = new URL(sb.toString());
  HttpURLConnection conn = (HttpURLConnection)url.openConnection();
  conn.setRequestMethod("GET");
  conn.setConnectTimeout(5 * 1000);
  if(conn.getResponseCode()==200){
   return true;
  }
  return false;
 }

抱歉!评论已关闭.