从HttpClient更新为HttpURLConnection

时间:2015-05-23 03:58:16

标签: java android forms httpclient httpurlconnection

我一直在使用Android应用,最近使用HttpClient获得了所有登录代码。但是,此代码现已弃用,不再有效。语言摘要建议将所有HttpClient代码替换为HttpURLConnection个对象。我不知道怎么做。如果有人可以帮助我将代码更改为HttpURLConnection以提交登录表单并检索信息,我们将不胜感激。这是我原来的使用功能代码:

String u = params[0];
String p = params[1];
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS,Boolean.TRUE);
//client.setRedirectStrategy(new LaxRedirectStrategy());
HttpPost post = new HttpPost("https://home-access.cfisd.net/HomeAccess/Account/LogOn");
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("LogOnDetails.Username", u));
list.add(new BasicNameValuePair("LogOnDetails.Password",p));
list.add(new BasicNameValuePair("Database","10"));
HttpResponse response = null;
try{
    post.setEntity(new UrlEncodedFormEntity(list));
    response = client.execute(post);
}
catch(Exception ex)
{
    ex.printStackTrace();
}

2 个答案:

答案 0 :(得分:1)

嘿兄弟得到像

这样的图书馆

androids默认值是旧的和笨重的

loopjs asynchttpclientokhttpKoushs ion

使用2-3行代码完成大量工作

loopjs async http示例

RequestParams params = new RequestParams();
params.put("key", "value");
params.put("more", "data");
AsyncHttpClient client = new AsyncHttpClient();
client.post(url, params, responseHandler);

//do something with response
@Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONArray response) {

        }
    });

答案 1 :(得分:0)

您可以获取连接的输出流并将参数查询字符串写入其中。

# these are the directories
vars <- c("clt", "hurs", "pr", "tas", "was")

# prepare progress info
pb <- txtProgressBar(min = 0, max = length(vars), style = 3)

# first, loop through variables
for (i in 1:length(vars)){

  # do some stuff here: each file takes a bit long to finish
  files <- c("was_Amon_CanESM2_historical_r1i1p1_185001-200512.nc", "was_Amon_CanESM2_historical_r1i1p1_185001-200512_SA.nc", 
  "was_Amon_GFDL-ESM2M_historical_r1i1p1_186101-200512.nc", "was_Amon_GFDL-ESM2M_historical_r1i1p1_186101-200512_SA.nc", 
  "was_Amon_GISS-E2-H_historical_r1i1p1_185001-200512.nc", "was_Amon_GISS-E2-H_historical_r1i1p1_185001-200512_SA.nc", 
  "was_Amon_GISS-E2-R-CC_historical_r1i1p1_185001-201012.nc", "was_Amon_GISS-E2-R-CC_historical_r1i1p1_185001-201012_SA.nc", 
  "was_Amon_GISS-E2-R_historical_r1i1p1_185001-200512.nc", "was_Amon_GISS-E2-R_historical_r1i1p1_185001-200512_SA.nc", 
  "was_Amon_HadGEM2-AO_historical_r1i1p1_186001-200512.nc", "was_Amon_HadGEM2-AO_historical_r1i1p1_186001-200512_SA.nc", 
  "was_Amon_HadGEM2-CC_historical_r1i1p1_185912-200511.nc", "was_Amon_HadGEM2-CC_historical_r1i1p1_185912-200511_SA.nc", 
  "was_Amon_IPSL-CM5A-MR_historical_r1i1p1_185001-200512.nc", "was_Amon_IPSL-CM5A-MR_historical_r1i1p1_185001-200512_SA.nc", 
  "was_Amon_IPSL-CM5B-LR_historical_r1i1p1_185001-200512.nc", "was_Amon_IPSL-CM5B-LR_historical_r1i1p1_185001-200512_SA.nc", 
  "was_Amon_MIROC4h_historical_r1i1p1_195001-200512.nc", "was_Amon_MIROC4h_historical_r1i1p1_195001-200512_SA.nc", 
  "was_Amon_MRI-CGCM3_historical_r1i1p1_185001-200512.nc", "was_Amon_MRI-CGCM3_historical_r1i1p1_185001-200512_SA.nc", 
  "was_Amon_NorESM1-M_historical_r1i1p1_185001-200512.nc", "was_Amon_NorESM1-M_historical_r1i1p1_185001-200512_SA.nc", 
  "was_Amon_bcc-csm1-1-m_historical_r1i1p1_185001-201212.nc", "was_Amon_bcc-csm1-1-m_historical_r1i1p1_185001-201212_SA.nc", 
  "was_Amon_bcc-csm1-1_historical_r1i1p1_185001-201212.nc", "was_Amon_bcc-csm1-1_historical_r1i1p1_185001-201212_SA.nc", 
  "was_Amon_inmcm4_historical_r1i1p1_185001-200512.nc", "was_Amon_inmcm4_historical_r1i1p1_185001-200512_SA.nc"
  )

  # loop through files
  for (j in files){

    # do some stuff here too

    # inform progress on files
    Sys.sleep(0.05)
    setTxtProgressBar(pb, i)

  }

}

...

URL url = new URL("http://yoururl.com");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

conn.setReadTimeout(10000);

conn.setConnectTimeout(15000);

conn.setRequestMethod("POST");

conn.setDoInput(true);

conn.setDoOutput(true);

List<NameValuePair> params = new ArrayList<NameValuePair>();

params.add(new BasicNameValuePair("firstParam", paramValue1));

params.add(new BasicNameValuePair("secondParam", paramValue2));

params.add(new BasicNameValuePair("thirdParam", paramValue3));

OutputStream os = conn.getOutputStream();

BufferedWriter writer = new BufferedWriter(
        new OutputStreamWriter(os, "UTF-8"));

writer.write(getQuery(params));

writer.flush();

writer.close();

os.close();

conn.connect();