android发送get请求

一、权限

<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;
 }

没有更多推荐了,返回首页