標籤:
在看了網路上非常多視頻關於android通過HTTP POST或者GET方式訪問網頁並擷取資料的方法。
自己也copy了一份來測試。並通過C#.NET搭建了一個簡單的後台,但發現傳參時,依照網上的方式來做無法得到對應的結果。
下面是我的求貼
http://bbs.csdn.net/topics/390814679
發了好久都沒有人關於回覆這個問題,預計大家都不是使用ASP.NET來做後台。
經過了重複的測試手機端代碼。發現事實上ASP.NET做的後台,事實上可以直接解析URL中帶參數,不須要通過網上介紹的方法實現
下面是截取測試代碼的主要部分:
button觸發:
<span style="white-space:pre"></span>final Button btn2 = (Button) findViewById(R.id.button2);btn2.setOnClickListener(new OnClickListener() {public void onClick(View v) {progressDialog = ProgressDialog.show(MainActivity.this,"載入中...", "請等待...", true, false);// 建立線程new Thread() {@Overridepublic void run() {// 須要花時間計算的方法try {String str = posturl("http://aspspider.info/lanjackg2003/Default.aspx?name=lan120576664&psw=456");textViewhttpRes.setText(str.toString());} catch (Exception e) {// TODO: handle exception}// 向handler發訊息handler.sendEmptyMessage(0);}}.start();}});擷取網頁資料的代碼:
public String posturl(String url){ InputStream is = null; String result = ""; try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); }catch(Exception e){ return "Fail to establish http connection!"+e.toString(); } try{ BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8")); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result=sb.toString(); Log.v(LOG_TAG,result.toString()); }catch(Exception e){ return "Fail to convert net stream!"; } return result; }
手機顯示
PC端顯示顯示:
PC與手機顯示的結果是一致的。
Android通過HTTP POST帶參訪問asp.net網頁