作者serv661 (cha)
看板AndroidDev
標題[問題] APP抓取webserver
時間Fri Nov 14 16:40:38 2014
只有一點點的程式基礎
最近想試著用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
→ x51811danny: json 是好物 11/15 00:00
推 givemepass: 你可能要了解一下Request跟response是什麼喔 11/15 09:31