綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[求助] 如何從MySQL資料庫撈資料呢?

[複製連結] 查看: 6026|回覆: 7|好評: 0
跳轉到指定樓層
樓主
wind8894 | 收聽TA | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
發表於 2012-3-20 22:10

馬上加入Android 台灣中文網,立即免費下載應用遊戲。

您需要 登錄 才可以下載或查看,沒有帳號?註冊

x
開發環境是

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     學生

欄位大概是這樣,想在手機端上撈到這筆資料,顯示在手機上面



「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)

評分

參與人數 1幫助 +1 收起 理由
wrt4511611 + 1 好內容。朕有賞,還不謝主隆恩.

查看全部評分

收藏收藏1 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

沙發
george310909 | 收聽TA | 只看該作者
發表於 2012-3-21 11:58
想請問一下
你知道怎麼用手機撈網頁原始碼嗎?
如果知道
這個可以用那個方法做
如果不知道
我晚一點附上相關程式碼
-----------------題外話-----------------
為什麼不用SQLite?
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

板凳
 樓主| wind8894 | 收聽TA | 只看該作者
發表於 2012-3-21 15:21
不知道怎撈耶,可以麻煩你PO程式碼嗎?感謝你~

SQLite不是內建的資料庫嗎?
能跟網頁結合?
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

地板
george310909 | 收聽TA | 只看該作者
發表於 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. }
複製代碼
以前做的
想當年應付指導教授很好用
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

5
george310909 | 收聽TA | 只看該作者
發表於 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. }
複製代碼
不好意思,我以為你只要存手機
以上是我以前看網路資料做的範例
至於內容有點忘記是看哪的

點評

我複製這段有幾行錯誤欸,能否把這程式的專案檔上傳呢?  發表於 2012-3-24 15:22

評分

參與人數 1幫助 +1 收起 理由
wind8894 + 1 偶像,看完你的內容,讓我找到了活著的意義.

查看全部評分

用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

6
george310909 | 收聽TA | 只看該作者
發表於 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. }
複製代碼
我說的方法,以前看別人網站時做的
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

7
ploglin | 收聽TA | 只看該作者
發表於 2012-4-2 10:13
如果你會寫PHP的話,應該自己寫一支API,網路上也很多現成的。
再用Android去讀取你的API網址,回傳的格式可以用xml或json,基本上用json比較方便。

如果你真的要去爬網頁上的 source code ,也可以用 java 的 jsoup 來達成,用法跟 jQeury 有點類似。
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

8
momosagax | 收聽TA | 只看該作者
發表於 2012-4-3 12:37
應該是要使用

import com.mysql.*
然後下mysql的指令來做使用

不過這類型的方式安全性相對性的比較低

建議還是使用撈php網頁資料~讓php呼叫sql資料就好
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則