[問題] APP抓取webserver

看板AndroidDev作者 (cha)時間11年前 (2014/11/14 16:40), 編輯推噓1(101)
留言2則, 2人參與, 最新討論串1/2 (看更多)
只有一點點的程式基礎 最近想試著用app寫程式抓MySQL資料庫 爬文查資料後也發現中間要透過webserver,才能抓資料庫 所以上網爬了很久,是有看到幾個範例跟做法 比較多的是用php去轉檔去寫,但我真的不太會使用php 有爬到幾個範例,以下是其中一個 package com.givemepass.getphpservermessage; import android.app.Activity; public class GetPhpServerMessageDemoActivity extends Activity { /** Called when the activity is first created. */ private TextView textView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textView = (TextView)findViewById(R.id.text_view); GetServerMessage message = new GetServerMessage(); String msg = message.stringQuery("http://localhost/WebService/Service1.asmx/getRecord"); textView.setText("Server message is "+msg); } } 以上是主程式 ---------------------- package com.givemepass.getphpservermessage; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; public class GetServerMessage { public String stringQuery(String url){ try { HttpClient httpclient = new DefaultHttpClient(); HttpPost method = new HttpPost(url); HttpResponse response = httpclient.execute(method); HttpEntity entity = response.getEntity(); if(entity != null){ return EntityUtils.toString(entity); } else{ return "No string."; } } catch(Exception e) { return "Network problem"; } } } 以上是副程式---------------- 這個範例應該是從wevserver去抓網頁字串 跑出來的會是網頁的原始檔 例如 <html> ... <body>等 可是我想從APP抓網頁抓的是資料的話,是不是還要從這些字串去解析成我想要的資料??? 而且我在 String msg = message.stringQuery("http://localhost/WebService/Service1.asmx/getRecord"); 這個地方我一直都連不到,但是我拿我同事之前把網頁轉成php的網址,卻可以顯示出來 字串 (其實這個範例原先放就是把網頁轉成php網址) 但是語法上有規定一定要php的網址嗎? 還是說這個htm不行直接放上去 ??? ---------------------------------- 我在網路上看到的範例,幾乎跑出來的都是有錯的 即使我想改成正確的,我也不太知道如何去改,因為範例編譯沒錯但跑出來都是已停止 請問可以教教我嗎,我真的沒有很厲害... 可以教教我怎麼連到webserver嗎 (or 私信) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.250.68.238 ※ 文章網址: http://www.ptt.cc/bbs/AndroidDev/M.1415954441.A.26B.html

11/15 00:00, , 1F
json 是好物
11/15 00:00, 1F

11/15 09:31, , 2F
你可能要了解一下Request跟response是什麼喔
11/15 09:31, 2F
文章代碼(AID): #1KPS099h (AndroidDev)
文章代碼(AID): #1KPS099h (AndroidDev)