我成功了!!!(印出DB 的JSON值但未做其他應用)
在此感謝  程式參考出處 http://seanstar5317.pixnet.net/blog/post/28092031-%5Bandroid%5D%E5%BE%9Emysql-%E6%8A%93%E8%B3%87%E6%96%99%EF%BC%8C%E5%8B%95%E6%85%8B%E6%96%B0%E5%A2%9Etextview%E8%87%B3tablela

此大大的文章  幫助了我很多  程式碼多出於此 但加了許多註解  及修改了部分
順到一提我是有一個SEVER供我測試  24HOST不過要錢可以用WAMP做測試 (不過要記得IP位置要打好)

首先 android連mysql 需要  android ->php->mysql-> php-> android(回傳結果) 

php檔

<?php

$sql = 'SELECT * FROM users';  //與原本大大不同的地方

?>

Gradle部分  (記得在23版後就取消了 所以gradle要加東西)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.vincent.testandroiddb"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'org.apache.httpcomponents:httpmime:4.3.5'
    //classpath 'com.android.tools.build:gradle:1.3.0'
}
android {
    useLibrary 'org.apache.http.legacy'
}


manifests的地方
 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.vincent.testandroiddb">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".TestAndroidDBActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.INTERNET" />
</manifest>


            try {
                String result = DBConnector.executeQuery("SELECT * FROM users");
                textView01.setText(result);
                //印出json這裡可以看出來 有無連線
                /*
                    SQL 結果有多筆資料時使用JSONArray
                    只有一筆資料時直接建立JSONObject物件
                    JSONObject jsonData = new JSONObject(result);
                */
                //////////////////////////這裡我無法執行 但我還是放著日後有空會想辦法解決這個問題

結果

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 晨柚 的頭像
    晨柚

    晨柚的部落格

    晨柚 發表在 痞客邦 留言(2) 人氣()