Android 台灣中文網
標題:
如何從MySQL資料庫撈資料呢?
[打印本頁]
作者:
wind8894
時間:
2012-3-20 22:10
標題:
如何從MySQL資料庫撈資料呢?
開發環境是
MySQL+phpMyAdmin+Apache/2.2.21 +PHP/5.3.10
在MySQL資料庫內已經有表單、資料了,想知道要如何寫一個android的程式去撈到這筆資料
no name sex age star_signs height weight career
1 孫小美 女 15 ~ 20 雙魚座 165 ~ 170 50 ~ 55 學生
欄位大概是這樣,想在手機端上撈到這筆資料,顯示在手機上面
作者:
george310909
時間:
2012-3-21 11:58
想請問一下
你知道怎麼用手機撈網頁原始碼嗎?
如果知道
這個可以用那個方法做
如果不知道
我晚一點附上相關程式碼
-----------------題外話-----------------
為什麼不用SQLite?
作者:
wind8894
時間:
2012-3-21 15:21
不知道怎撈耶,可以麻煩你PO程式碼嗎?感謝你~
SQLite不是內建的資料庫嗎?
能跟網頁結合?
作者:
george310909
時間:
2012-3-21 21:07
本帖最後由 george310909 於 2012-3-21 21:35 編輯
喔,原來你是要跟網頁結合,那不好意思,我誤會了
package test.gl;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class testgl extends Activity {
private Button mButton1;
private TextView mTextView1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton1 = (Button) findViewById(R.id.myButton1);
mTextView1 = (TextView) findViewById(R.id.myTextView1);
mButton1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
String uriAPI = "http://www.google.com.tw";
HttpPost httpRequest = new HttpPost(uriAPI);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("str", "I am Post String"));
try {
httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
//接收原始碼的變數
String strResult = EntityUtils.toString(httpResponse.getEntity());
mTextView1.setText(strResult);
} else {
mTextView1.setText("Error Response: "+ httpResponse.getStatusLine().toString());
}
} catch (ClientProtocolException e) {
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (IOException e) {
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (Exception e) {
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
}
});
}
public String eregi_replace(String strFrom, String strTo, String strTarget) {
String strPattern = "()" + strFrom;
Pattern p = Pattern.compile(strPattern);
Matcher m = p.matcher(strTarget);
if (m.find()) {
return strTarget.replaceAll(strFrom, strTo);
} else {
return strTarget;
}
}
}
複製代碼
以前做的
想當年應付指導教授很好用
作者:
george310909
時間:
2012-3-21 21:36
package test.gl;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class testgl extends Activity {
private Button mButton1;
private TextView mTextView1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton1 = (Button) findViewById(R.id.myButton1);
mTextView1 = (TextView) findViewById(R.id.myTextView1);
mButton1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
String uriAPI = "http://www.google.com.tw";
HttpPost httpRequest = new HttpPost(uriAPI);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("str", "I am Post String"));
try {
httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
//接收原始碼的變數
String strResult = EntityUtils.toString(httpResponse.getEntity());
mTextView1.setText(strResult);
} else {
mTextView1.setText("Error Response: "+ httpResponse.getStatusLine().toString());
}
} catch (ClientProtocolException e) {
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (IOException e) {
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (Exception e) {
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
}
});
}
public String eregi_replace(String strFrom, String strTo, String strTarget) {
String strPattern = "()" + strFrom;
Pattern p = Pattern.compile(strPattern);
Matcher m = p.matcher(strTarget);
if (m.find()) {
return strTarget.replaceAll(strFrom, strTo);
} else {
return strTarget;
}
}
}
複製代碼
不好意思,我以為你只要存手機
以上是我以前看網路資料做的範例
至於內容有點忘記是看哪的
作者:
george310909
時間:
2012-3-21 21:39
package test.gl;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class testgl extends Activity {
private Button mButton1;
private TextView mTextView1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton1 = (Button) findViewById(R.id.myButton1);
mTextView1 = (TextView) findViewById(R.id.myTextView1);
mButton1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
String uriAPI = "http://www.google.com.tw";
HttpPost httpRequest = new HttpPost(uriAPI);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("str", "I am Post String"));
try {
httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
//接收原始碼的變數
String strResult = EntityUtils.toString(httpResponse.getEntity());
mTextView1.setText(strResult);
} else {
mTextView1.setText("Error Response: "+ httpResponse.getStatusLine().toString());
}
} catch (ClientProtocolException e) {
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (IOException e) {
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (Exception e) {
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
}
});
}
public String eregi_replace(String strFrom, String strTo, String strTarget) {
String strPattern = "()" + strFrom;
Pattern p = Pattern.compile(strPattern);
Matcher m = p.matcher(strTarget);
if (m.find()) {
return strTarget.replaceAll(strFrom, strTo);
} else {
return strTarget;
}
}
}
複製代碼
我說的方法,以前看別人網站時做的
作者:
ploglin
時間:
2012-4-2 10:13
如果你會寫PHP的話,應該自己寫一支API,網路上也很多現成的。
再用Android去讀取你的API網址,回傳的格式可以用xml或json,基本上用json比較方便。
如果你真的要去爬網頁上的 source code ,也可以用 java 的 jsoup 來達成,用法跟 jQeury 有點類似。
作者:
momosagax
時間:
2012-4-3 12:37
應該是要使用
import com.mysql.*
然後下mysql的指令來做使用
不過這類型的方式安全性相對性的比較低
建議還是使用撈php網頁資料~讓php呼叫sql資料就好
歡迎光臨 Android 台灣中文網 (https://apk.tw/)
Powered by Discuz! X3.1