作者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://webptt.com/cn.aspx?n=bbs/AndroidDev/M.1415954441.A.26B.html
1F:→ x51811danny: json 是好物 11/15 00:00
2F:推 givemepass: 你可能要了解一下Request跟response是什麽喔 11/15 09:31