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 編輯

喔,原來你是要跟網頁結合,那不好意思,我誤會了

  1. package test.gl;

  2. import org.apache.http.HttpResponse;
  3. import org.apache.http.NameValuePair;
  4. import org.apache.http.client.ClientProtocolException;
  5. import org.apache.http.client.entity.UrlEncodedFormEntity;
  6. import org.apache.http.client.methods.HttpPost;
  7. import org.apache.http.impl.client.DefaultHttpClient;
  8. import org.apache.http.message.BasicNameValuePair;
  9. import org.apache.http.protocol.HTTP;
  10. import org.apache.http.util.EntityUtils;
  11. import java.io.IOException;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.regex.Matcher;
  15. import java.util.regex.Pattern;

  16. import android.app.Activity;
  17. import android.os.Bundle;
  18. import android.view.View;
  19. import android.widget.Button;
  20. import android.widget.TextView;

  21. public class testgl extends Activity {
  22.         private Button mButton1;
  23.         private TextView mTextView1;

  24.         @Override
  25.         public void onCreate(Bundle savedInstanceState) {
  26.                 super.onCreate(savedInstanceState);
  27.                 setContentView(R.layout.main);
  28.                 mButton1 = (Button) findViewById(R.id.myButton1);
  29.                 mTextView1 = (TextView) findViewById(R.id.myTextView1);
  30.                 mButton1.setOnClickListener(new Button.OnClickListener() {
  31.                         @Override
  32.                         public void onClick(View v) {
  33.                                 String uriAPI = "http://www.google.com.tw";
  34.                                 HttpPost httpRequest = new HttpPost(uriAPI);
  35.                                 List<NameValuePair> params = new ArrayList<NameValuePair>();
  36.                                 params.add(new BasicNameValuePair("str", "I am Post String"));
  37.                                 try {
  38.                                         httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
  39.                                         HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
  40.                                         if (httpResponse.getStatusLine().getStatusCode() == 200) {
  41.                                                 //接收原始碼的變數
  42.                                                 String strResult = EntityUtils.toString(httpResponse.getEntity());
  43.                                                 mTextView1.setText(strResult);
  44.                                         } else {
  45.                                                 mTextView1.setText("Error Response: "+ httpResponse.getStatusLine().toString());
  46.                                         }
  47.                                 } catch (ClientProtocolException e) {
  48.                                         mTextView1.setText(e.getMessage().toString());
  49.                                         e.printStackTrace();
  50.                                 } catch (IOException e) {
  51.                                         mTextView1.setText(e.getMessage().toString());
  52.                                         e.printStackTrace();
  53.                                 } catch (Exception e) {
  54.                                         mTextView1.setText(e.getMessage().toString());
  55.                                         e.printStackTrace();
  56.                                 }

  57.                         }
  58.                 });
  59.         }

  60.         public String eregi_replace(String strFrom, String strTo, String strTarget) {
  61.                 String strPattern = "()" + strFrom;
  62.                 Pattern p = Pattern.compile(strPattern);
  63.                 Matcher m = p.matcher(strTarget);
  64.                 if (m.find()) {
  65.                         return strTarget.replaceAll(strFrom, strTo);
  66.                 } else {
  67.                         return strTarget;
  68.                 }
  69.         }
  70. }
複製代碼
以前做的
想當年應付指導教授很好用
作者: george310909    時間: 2012-3-21 21:36
  1. package test.gl;

  2. import org.apache.http.HttpResponse;
  3. import org.apache.http.NameValuePair;
  4. import org.apache.http.client.ClientProtocolException;
  5. import org.apache.http.client.entity.UrlEncodedFormEntity;
  6. import org.apache.http.client.methods.HttpPost;
  7. import org.apache.http.impl.client.DefaultHttpClient;
  8. import org.apache.http.message.BasicNameValuePair;
  9. import org.apache.http.protocol.HTTP;
  10. import org.apache.http.util.EntityUtils;
  11. import java.io.IOException;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.regex.Matcher;
  15. import java.util.regex.Pattern;

  16. import android.app.Activity;
  17. import android.os.Bundle;
  18. import android.view.View;
  19. import android.widget.Button;
  20. import android.widget.TextView;

  21. public class testgl extends Activity {
  22.         private Button mButton1;
  23.         private TextView mTextView1;

  24.         @Override
  25.         public void onCreate(Bundle savedInstanceState) {
  26.                 super.onCreate(savedInstanceState);
  27.                 setContentView(R.layout.main);
  28.                 mButton1 = (Button) findViewById(R.id.myButton1);
  29.                 mTextView1 = (TextView) findViewById(R.id.myTextView1);
  30.                 mButton1.setOnClickListener(new Button.OnClickListener() {
  31.                         @Override
  32.                         public void onClick(View v) {
  33.                                 String uriAPI = "http://www.google.com.tw";
  34.                                 HttpPost httpRequest = new HttpPost(uriAPI);
  35.                                 List<NameValuePair> params = new ArrayList<NameValuePair>();
  36.                                 params.add(new BasicNameValuePair("str", "I am Post String"));
  37.                                 try {
  38.                                         httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
  39.                                         HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
  40.                                         if (httpResponse.getStatusLine().getStatusCode() == 200) {
  41.                                                 //接收原始碼的變數
  42.                                                 String strResult = EntityUtils.toString(httpResponse.getEntity());
  43.                                                 mTextView1.setText(strResult);
  44.                                         } else {
  45.                                                 mTextView1.setText("Error Response: "+ httpResponse.getStatusLine().toString());
  46.                                         }
  47.                                 } catch (ClientProtocolException e) {
  48.                                         mTextView1.setText(e.getMessage().toString());
  49.                                         e.printStackTrace();
  50.                                 } catch (IOException e) {
  51.                                         mTextView1.setText(e.getMessage().toString());
  52.                                         e.printStackTrace();
  53.                                 } catch (Exception e) {
  54.                                         mTextView1.setText(e.getMessage().toString());
  55.                                         e.printStackTrace();
  56.                                 }

  57.                         }
  58.                 });
  59.         }

  60.         public String eregi_replace(String strFrom, String strTo, String strTarget) {
  61.                 String strPattern = "()" + strFrom;
  62.                 Pattern p = Pattern.compile(strPattern);
  63.                 Matcher m = p.matcher(strTarget);
  64.                 if (m.find()) {
  65.                         return strTarget.replaceAll(strFrom, strTo);
  66.                 } else {
  67.                         return strTarget;
  68.                 }
  69.         }
  70. }
複製代碼
不好意思,我以為你只要存手機
以上是我以前看網路資料做的範例
至於內容有點忘記是看哪的
作者: george310909    時間: 2012-3-21 21:39
  1. package test.gl;

  2. import org.apache.http.HttpResponse;
  3. import org.apache.http.NameValuePair;
  4. import org.apache.http.client.ClientProtocolException;
  5. import org.apache.http.client.entity.UrlEncodedFormEntity;
  6. import org.apache.http.client.methods.HttpPost;
  7. import org.apache.http.impl.client.DefaultHttpClient;
  8. import org.apache.http.message.BasicNameValuePair;
  9. import org.apache.http.protocol.HTTP;
  10. import org.apache.http.util.EntityUtils;
  11. import java.io.IOException;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.regex.Matcher;
  15. import java.util.regex.Pattern;

  16. import android.app.Activity;
  17. import android.os.Bundle;
  18. import android.view.View;
  19. import android.widget.Button;
  20. import android.widget.TextView;

  21. public class testgl extends Activity {
  22.         private Button mButton1;
  23.         private TextView mTextView1;

  24.         @Override
  25.         public void onCreate(Bundle savedInstanceState) {
  26.                 super.onCreate(savedInstanceState);
  27.                 setContentView(R.layout.main);
  28.                 mButton1 = (Button) findViewById(R.id.myButton1);
  29.                 mTextView1 = (TextView) findViewById(R.id.myTextView1);
  30.                 mButton1.setOnClickListener(new Button.OnClickListener() {
  31.                         @Override
  32.                         public void onClick(View v) {
  33.                                 String uriAPI = "http://www.google.com.tw";
  34.                                 HttpPost httpRequest = new HttpPost(uriAPI);
  35.                                 List<NameValuePair> params = new ArrayList<NameValuePair>();
  36.                                 params.add(new BasicNameValuePair("str", "I am Post String"));
  37.                                 try {
  38.                                         httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
  39.                                         HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
  40.                                         if (httpResponse.getStatusLine().getStatusCode() == 200) {
  41.                                                 //接收原始碼的變數
  42.                                                 String strResult = EntityUtils.toString(httpResponse.getEntity());
  43.                                                 mTextView1.setText(strResult);
  44.                                         } else {
  45.                                                 mTextView1.setText("Error Response: "+ httpResponse.getStatusLine().toString());
  46.                                         }
  47.                                 } catch (ClientProtocolException e) {
  48.                                         mTextView1.setText(e.getMessage().toString());
  49.                                         e.printStackTrace();
  50.                                 } catch (IOException e) {
  51.                                         mTextView1.setText(e.getMessage().toString());
  52.                                         e.printStackTrace();
  53.                                 } catch (Exception e) {
  54.                                         mTextView1.setText(e.getMessage().toString());
  55.                                         e.printStackTrace();
  56.                                 }

  57.                         }
  58.                 });
  59.         }

  60.         public String eregi_replace(String strFrom, String strTo, String strTarget) {
  61.                 String strPattern = "()" + strFrom;
  62.                 Pattern p = Pattern.compile(strPattern);
  63.                 Matcher m = p.matcher(strTarget);
  64.                 if (m.find()) {
  65.                         return strTarget.replaceAll(strFrom, strTo);
  66.                 } else {
  67.                         return strTarget;
  68.                 }
  69.         }
  70. }
複製代碼
我說的方法,以前看別人網站時做的

作者: 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