java操作new HttpPost(URL)和 new URL(shixun_ip) 獲取流的方法?這個(gè)問(wèn)題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見(jiàn)到的。希望通過(guò)這個(gè)問(wèn)題能讓你收獲頗深。下面是小編給大家?guī)?lái)的參考內(nèi)容,讓我們一起來(lái)看看吧!
創(chuàng)新互聯(lián)公司主營(yíng)洪雅網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,app軟件定制開(kāi)發(fā),洪雅h5微信小程序搭建,洪雅網(wǎng)站營(yíng)銷推廣歡迎洪雅等地區(qū)企業(yè)咨詢
HttpClient:
private static HttpClient httpClient; static { if (httpClient == null) { HttpParams params = new BasicHttpParams(); // 設(shè)置一些基本參數(shù) HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); // 超時(shí)設(shè)置 /* 從連接池中取連接的超時(shí)時(shí)間 */ ConnManagerParams.setTimeout(params, 1000); /* 連接超時(shí) */ HttpConnectionParams.setConnectionTimeout(params, 2000); /* 請(qǐng)求超時(shí) */ HttpConnectionParams.setSoTimeout(params, 4000); SchemeRegistry schReg = new SchemeRegistry(); schReg.register(new Scheme("http", PlainSocketFactory .getSocketFactory(), 80)); schReg.register(new Scheme("https", SSLSocketFactory .getSocketFactory(), 443)); // 使用線程安全的連接管理來(lái)創(chuàng)建HttpClient ClientConnectionManager conMgr = new ThreadSafeClientConnManager( params, schReg); httpClient = new DefaultHttpClient(conMgr, params); } } @SuppressWarnings("unchecked") privateT getJsonObjectFromUrl(String urlStr, List params, Class className) throws Exception{ T object = null; InputStream in = null; try { HttpPost httpPost = new HttpPost(urlStr); if (params != null) { // List nvps = new ArrayList (); // for (Entry entry : params.entrySet()) { // nvps.add(new BasicNameValuePair(entry.getKey(), entry // .getValue())); // } httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); } HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); if(entity!=null){ in= entity.getContent(); //之前沒(méi)使用這個(gè)造成了大量異常拋出 } /*String s = EntityUtils.toString(in, "UTF-8"); if (entity != null) { EntityUtils.consume(entity); }*/ String s = inputStream2String(in,"UTF-8"); JsonResult jsonResult = (JsonResult)JSONObject.parseObject(s, JsonResult.class); if(jsonResult.getCode() == 200){ Object obj = jsonResult.getData(); if(obj != null){ if(className.isInstance(obj)){ object = (T)(obj); } else if(obj instanceof JSONObject){ object= JSONObject.parseObject(obj.toString(), className); } } }else{ throw new Exception(jsonResult.getMsg()); } if(in!=null){ in.close(); } } catch (Exception e) { throw e; } return object; }
new URL():
try{ //省略 URL shixun = new URL(url); InputStream in = shixun.openStream(); long end = System.currentTimeMillis(); if((end-start)<=5000){ //5s內(nèi)不響應(yīng)就跳過(guò) String ret = cn.com.jsoft.jframe.utils.StringUtils.toString(in, "utf-8"); JSONObject obj = JSONObject.fromObject(ret); if(obj!=null&&obj.getJSONObject("info")!=null&&obj.containsKey("info")==true){ JSONObject info = obj.getJSONObject("info"); JSONArray data = info.getJSONArray("data"); if(data!=null&&data.size()>0){ for(int i=0;ijava將InputStream轉(zhuǎn)為String
public static String inputStream2String(InputStream is, String charset) { ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); int i = -1; while ((i = is.read()) != -1) { baos.write(i); } return baos.toString(charset); } catch (IOException e) { e.printStackTrace(); logger.error("FileWRUtil.inputStream2String(InputStream is, String charset) occur error:" + e.getMessage()); } finally { if (null != baos) { try { baos.close(); } catch (IOException e) { e.printStackTrace(); logger.error("FileWRUtil.inputStream2String(InputStream is, String charset) occur error:" + e.getMessage()); } baos = null; } } return null; }感謝各位的閱讀!看完上述內(nèi)容,你們對(duì)java操作new HttpPost(URL)和 new URL(shixun_ip) 獲取流的方法大概了解了嗎?希望文章內(nèi)容對(duì)大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前文章:java操作newHttpPost(URL)和newURL(shixun_ip)獲取流的方法
新聞來(lái)源:http://weahome.cn/article/jpgohg.html