1.如何让Android获得网页上的数据
2.cad vba 样条曲线坐标
3.HttpClientåHttpURLConnectionçåºå«
4.å¦ä½ä½¿ç¨HttpClient
如何让Android获得网页上的数据
例子来自于android学习手册,android学习手册包含9个章节,个例子,酒类app源码源码文档随便看,例子都是永久伊甸园源码可交互,可运行,秒传源码源码采用android studio目录结构,高亮显示代码,文档都采用文档结构图显示,可以快速定位。手机助手中下载,图标上有贝壳
//第一种
/**获取参数(ArrayList<NameValuePair> nameValuePairs,String url)后post给远程服务器
* 将获得的返回结果(String)返回给调用者
* 本函数适用于查询数量较少的时候
*/
public String posturl(ArrayList<NameValuePair> nameValuePairs,String url){
String result = "";
String tmp= "";
InputStream is = null;
try{
HttpClient stream!";
}
try{
JSONArray jArray = new JSONArray(tmp);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Iterator<?> keys=json_data.keys();
while(keys.hasNext()){
result += json_data.getString(keys.next().toString());
}
}
}catch(JSONException e){
return "The URL you post is wrong!";
}
return result;
}
cad vba 样条曲线坐标
简单:首先要选择该样条曲线方法有两种,我就说说简单的一种!
Sub www()
Dim myspl As AcadSpline
Dim selobj As Object
Dim ppt As Variant
’以下do while 语句仅仅是互动教学源码为了避免选择样条曲线时出错。如若简单点的话可以改成
'ThisDrawing.Utility.GetEntity selobj, ppt, "请选择样条曲线"
'Set myspl = selobj
Do While code = 0
On Error Resume Next
ThisDrawing.Utility.GetEntity selobj, ppt, "请选择样条曲线"
If Err <> 0 Then
Err.Clear
ThisDrawing.Utility.Prompt " 没有选定样条曲线对象,退出"
Exit Sub
End If
If Err.Number = 0 Then
If (selobj.EntityName = "AcDbSpline") Then
Set myspl = selobj ‘将获取的曲线赋给变量myspl
Exit Do
End If
Err.Clear
End If
Loop
Dim np As Integer
Dim pl() As Double
Dim pt As Variant
np = myspl.NumberOfFitPoints ’获取样条曲线的点数
ReDim pl(0 To np * 3 - 1)
Open "c:\1.txt" For Output As #1
For i = 0 To np - 1
pt = myspl.GetFitPoint(i) ‘获取样条曲线第i个点
For j = 0 To 2
pl(i * 3 + j) = pt(j)
Next
Print #1, pt(0), pt(1), pt(2) ’输出样条曲线的点坐标到.txt中
Next
ThisDrawing.ModelSpace.Add3DPoly pl ‘ 将样条曲线的点连成多段线
Close #1
ZoomAll
End Sub
HttpClientåHttpURLConnectionçåºå«
æ»ç»äºç½ä¸çä¸äºèµæºï¼ä¸»è¦æ以ä¸ä¸¤ä¸ªè§ç¹ï¼
åæä¸ï¼
å¨ç 究Volleyæ¡æ¶çæºç ä¸ï¼åç°å®å¨HTTP请æ±ç使ç¨ä¸æ¯è¾æææï¼å¨Android 2.3å以ä¸çæ¬ï¼ä½¿ç¨çæ¯HttpURLConnectionï¼èå¨Android 2.2å以ä¸çæ¬ï¼ä½¿ç¨çæ¯HttpClientãæä¹æ¯è¾å¥½å¥è¿ä¹ä½¿ç¨çåå ï¼äºæ¯ä¸é¨æ¾å°äºä¸ä½Googleçå·¥ç¨å¸åçä¸ç¯å客ï¼æä¸å¯¹HttpURLConnectionåHttpClientè¿è¡äºå¯¹æ¯ï¼ä¸é¢æå°±ç»å¤§å®¶ç®è¦å°ç¿»è¯ä¸ä¸ã
大å¤æ°çAndroidåºç¨ç¨åºé½ä¼ä½¿ç¨HTTPåè®®æ¥åéåæ¥æ¶ç½ç»æ°æ®ï¼èAndroidä¸ä¸»è¦æä¾äºä¸¤ç§æ¹å¼æ¥è¿è¡HTTPæä½ï¼HttpURLConnectionåHttpClientãè¿ä¸¤ç§æ¹å¼é½æ¯æHTTPSåè®®ã以æµçå½¢å¼è¿è¡ä¸ä¼ åä¸è½½ãé ç½®è¶ æ¶æ¶é´ãIPv6ã以åè¿æ¥æ± çåè½ã
HttpClient:
DefaultHttpClientåå®çå å¼AndroidHttpClienté½æ¯HttpClientå ·ä½çå®ç°ç±»ï¼å®ä»¬é½æ¥æä¼å¤çAPIï¼èä¸å®ç°æ¯è¾ç¨³å®ï¼bugæ°éä¹å¾å°ã
ä½åæ¶ä¹ç±äºHttpClientçAPIæ°éè¿å¤ï¼ä½¿å¾æ们å¾é¾å¨ä¸ç ´åå ¼å®¹æ§çæ åµä¸å¯¹å®è¿è¡å级åæ©å±ï¼æ以ç®åAndroidå¢éå¨æååä¼åHttpClientæ¹é¢çå·¥ä½æ度并ä¸ç§¯æã
HttpURLConnection:
HttpURLConnectionæ¯ä¸ç§å¤ç¨éãè½»éæçHTTP客æ·ç«¯ï¼ä½¿ç¨å®æ¥è¿è¡HTTPæä½å¯ä»¥éç¨äºå¤§å¤æ°çåºç¨ç¨åºãè½ç¶HttpURLConnectionçAPIæä¾çæ¯è¾ç®åï¼ä½æ¯åæ¶è¿ä¹ä½¿å¾æ们å¯ä»¥æ´å 容æå°å»ä½¿ç¨åæ©å±å®ã
ä¸è¿å¨Android 2.2çæ¬ä¹åï¼HttpURLConnectionä¸ç´åå¨çä¸äºä»¤äººåç¦çbugãæ¯å¦è¯´å¯¹ä¸ä¸ªå¯è¯»çInputStreamè°ç¨close()æ¹æ³æ¶ï¼å°±æå¯è½ä¼å¯¼è´è¿æ¥æ± 失æäºãé£ä¹æ们é常ç解å³åæ³å°±æ¯ç´æ¥ç¦ç¨æè¿æ¥æ± çåè½ï¼
[java] view plaincopy
[java] view plaincopy
private void disableConnectionReuseIfNecessary() {
// è¿æ¯ä¸ä¸ª2.2çæ¬ä¹åçbug
if (Integer.parseInt(Build.VERSION.SDK) < Build.VERSION_CODES.FROYO) {
System.setProperty(". ä¸ä½¿ç¨å¾æå¤ãæéè¦çåè®®äºï¼è¶æ¥è¶å¤ç Java åºç¨ç¨åºéè¦ç´æ¥éè¿ HTTP åè®®æ¥è®¿é®ç½ç»èµæºãå¨ JDK ç java.net å ä¸å·²ç»æä¾äºè®¿é® HTTP åè®®çåºæ¬åè½ï¼HttpURLConnectionã
HttpURLConnectionæ¯javaçæ åç±»ï¼HttpURLConnection继æ¿èªURLConnectionï¼å¯ç¨äºåæå®ç½ç«åéGET请æ±ãPOST请æ±ãå®å¨URLConnectionçåºç¡ä¸æä¾äºå¦ä¸ä¾¿æ·çæ¹æ³ï¼
int getResponseCode()ï¼è·åæå¡å¨çååºä»£ç ã
String getResponseMessage()ï¼è·åæå¡å¨çååºæ¶æ¯ã
String getResponseMethod()ï¼è·ååé请æ±çæ¹æ³ã
void setRequestMethod(String method)ï¼è®¾ç½®åé请æ±çæ¹æ³ã
å¨ä¸è¬æ åµä¸ï¼å¦æåªæ¯éè¦Webç«ç¹çæ个ç®å页é¢æ交请æ±å¹¶è·åæå¡å¨ååºï¼HttpURLConnectionå®å ¨å¯ä»¥èä»»ãä½å¨ç»å¤§é¨åæ åµä¸ï¼Webç«ç¹çç½é¡µå¯è½æ²¡è¿ä¹ç®åï¼è¿äºé¡µé¢å¹¶ä¸æ¯éè¿ä¸ä¸ªç®åçURLå°±å¯è®¿é®çï¼å¯è½éè¦ç¨æ·ç»å½èä¸å ·æç¸åºçæéæå¯è®¿é®è¯¥é¡µé¢ãå¨è¿ç§æ åµä¸ï¼å°±éè¦æ¶åSessionãCookieçå¤çäºï¼å¦ææç®ä½¿ç¨HttpURLConnectionæ¥å¤çè¿äºç»èï¼å½ç¶ä¹æ¯å¯è½å®ç°çï¼åªæ¯å¤çèµ·æ¥é¾åº¦å°±å¤§äºã
为äºæ´å¥½å°å¤çåWebç«ç¹è¯·æ±ï¼å æ¬å¤çSessionãCookieçç»èé®é¢ï¼Apacheå¼æºç»ç»æä¾äºä¸ä¸ªHttpClient项ç®ï¼çå®çå称就ç¥éï¼å®æ¯ä¸ä¸ªç®åçHTTP客æ·ç«¯ï¼å¹¶ä¸æ¯æµè§å¨ï¼ï¼å¯ä»¥ç¨äºåéHTTP请æ±ï¼æ¥æ¶HTTPååºãä½ä¸ä¼ç¼åæå¡å¨çååºï¼ä¸è½æ§è¡HTML页é¢ä¸åµå ¥çJavascript代ç ï¼ä¹ä¸ä¼å¯¹é¡µé¢å 容è¿è¡ä»»ä½è§£æãå¤çã
ç®åæ¥è¯´ï¼HttpClientå°±æ¯ä¸ä¸ªå¢å¼ºççHttpURLConnectionï¼HttpURLConnectionå¯ä»¥åçäºæ HttpClientå ¨é¨å¯ä»¥åï¼HttpURLConnection没ææä¾çæäºåè½ï¼HttpClientä¹æä¾äºï¼ä½å®åªæ¯å ³æ³¨äºå¦ä½åé请æ±ãæ¥æ¶
ååºï¼ä»¥å管çHTTPè¿æ¥ã
使ç¨HttpClientåé请æ±ãæ¥æ¶ååºå¾ç®åï¼åªè¦å¦ä¸å æ¥å³å¯ã
å建HttpClient对象ã
å¦æéè¦åéGET请æ±ï¼å建HttpGet对象ï¼å¦æéè¦åéPOST请æ±ï¼å建HttpPost对象ã
å¦æéè¦åé请æ±åæ°ï¼å¯è°ç¨HttpGetãHttpPostå ±åçsetParams(HetpParams params)æ¹æ³æ¥æ·»å 请æ±åæ°ï¼å¯¹äºHttpPost对象èè¨ï¼ä¹å¯è°ç¨setEntity(HttpEntity entity)æ¹æ³æ¥è®¾ç½®è¯·æ±åæ°ã
è°ç¨HttpClient对象çexecute(HttpUriRequest request)åé请æ±ï¼æ§è¡è¯¥æ¹æ³è¿åä¸ä¸ªHttpResponseã
è°ç¨HttpResponseçgetAllHeaders()ãgetHeaders(String name)çæ¹æ³å¯è·åæå¡å¨çååºå¤´ï¼è°ç¨HttpResponseçgetEntity()æ¹æ³å¯è·åHttpEntity对象ï¼è¯¥å¯¹è±¡å è£ äºæå¡å¨çååºå 容ãç¨åºå¯éè¿è¯¥å¯¹è±¡è·åæå¡å¨çååºå 容ã
å¦å¤ï¼Androidå·²ç»æåå°éæäºHttpClientï¼è¿æå³çå¼å人åå¯ä»¥ç´æ¥å¨Androidåºç¨ä¸ä½¿ç¨Httpclientæ¥è®¿é®æ交请æ±ãæ¥æ¶ååºã
æ¯å¦ä¸ä¸ªAndroidåºç¨éè¦åæå®é¡µé¢åé请æ±ï¼ä½è¯¥é¡µé¢å¹¶ä¸æ¯ä¸ä¸ªç®åç页é¢ï¼åªæå½ç¨æ·å·²ç»ç»å½ï¼èä¸ç»å½ç¨æ·çç¨æ·åæææ¶æå¯è®¿é®è¯¥é¡µé¢ãå¦æ使ç¨HttpURLConnectionæ¥è®¿é®è¿ä¸ªè¢«ä¿æ¤ç页é¢ï¼é£ä¹éè¦å¤ççç»è就太å¤æäºã
å ¶å®è®¿é®Webåºç¨ä¸è¢«ä¿æ¤ç页é¢ï¼ä½¿ç¨æµè§å¨åååç®åï¼ç¨æ·éè¿ç³»ç»æä¾çç»å½é¡µé¢ç»å½ç³»ç»ï¼æµè§å¨ä¼è´è´£ç»´æ¤ä¸æå¡å¨ä¹é´çSesionï¼å¦æç¨æ·ç»å½çç¨æ·åãå¯ç 符åè¦æ±ï¼å°±å¯ä»¥è®¿é®è¢«ä¿æ¤èµæºäºã
å¨Androidåºç¨ç¨åºä¸ï¼åå¯ä½¿ç¨HttpClientæ¥ç»å½ç³»ç»ï¼åªè¦åºç¨ç¨åºä½¿ç¨åä¸ä¸ªHttpClientåé请æ±ï¼HttpClientä¼èªå¨ç»´æ¤ä¸æå¡å¨ä¹é´çSessionç¶æï¼ä¹å°±æ¯è¯´ç¨åºç¬¬ä¸æ¬¡ä½¿ç¨HttpClientç»å½ç³»ç»åï¼æ¥ä¸æ¥ä½¿ç¨HttpClientå³å¯è®¿é®è¢«ä¿æ¤é¡µèäºã
å¦ä½ä½¿ç¨HttpClient
ãã1. å建HttpClient对象ã
ãã2. å建请æ±æ¹æ³çå®ä¾ï¼å¹¶æå®è¯·æ±URLãå¦æéè¦åéGET请æ±ï¼å建HttpGet对象ï¼å¦æéè¦åéPOST请æ±ï¼å建HttpPost对象ã
ãã3. å¦æéè¦åé请æ±åæ°ï¼å¯è°ç¨HttpGetãHttpPostå ±åçsetParams(HetpParams params)æ¹æ³æ¥æ·»å 请æ±åæ°ï¼å¯¹äºHttpPost对象èè¨ï¼ä¹å¯è°ç¨setEntity(HttpEntity entity)æ¹æ³æ¥è®¾ç½®è¯·æ±åæ°ã
ãã4. è°ç¨HttpClient对象çexecute(HttpUriRequest request)åé请æ±ï¼è¯¥æ¹æ³è¿åä¸ä¸ªHttpResponseã
ãã5. è°ç¨HttpResponseçgetAllHeaders()ãgetHeaders(String name)çæ¹æ³å¯è·åæå¡å¨çååºå¤´ï¼è°ç¨HttpResponseçgetEntity()æ¹æ³å¯è·åHttpEntity对象ï¼è¯¥å¯¹è±¡å è£ äºæå¡å¨çååºå 容ãç¨åºå¯éè¿è¯¥å¯¹è±¡è·åæå¡å¨çååºå 容ã
ãã6. éæ¾è¿æ¥ãæ 论æ§è¡æ¹æ³æ¯å¦æåï¼é½å¿ é¡»éæ¾è¿æ¥