Android App未连接到MySql数据库

时间:2017-03-17 16:32:30

标签: java php android mysql android-studio

我的Android应用中没有输出,没有任何事情发生。 PHP脚本在我的伙伴PC上,我们的笔记本电脑与交换机连接。当您在网站上打开PHP脚本时,您只看到一个空数组。

import android.os.AsyncTask;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class ActivityDataSource extends AsyncTask<String, Void, String> {

    public static final String AUTHKEY = "test321";

    public static final String POST_PARAM_KEYVALUE_SEPARATOR = "=";
    public static final String POST_PARAM_SEPARATOR = "&";

    private static final String DESTINATION_METHOD = "allEntrys";

    private TextView textView;

    private  URLConnection conn;

    public ActivityDataSource(TextView textView) {
        this.textView = textView;
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            openConnection();
            return readResult();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }


    private void openConnection() throws IOException{
        //StringBuffer für das zusammensetzen der URL
        StringBuffer dataBuffer = new StringBuffer();
        dataBuffer.append(URLEncoder.encode("authkey", "UTF-8"));
        dataBuffer.append(POST_PARAM_KEYVALUE_SEPARATOR);
        dataBuffer.append(URLEncoder.encode(AUTHKEY, "UTF-8"));
        dataBuffer.append(POST_PARAM_SEPARATOR);
        dataBuffer.append(URLEncoder.encode("method", "UTF-8"));
        dataBuffer.append(POST_PARAM_KEYVALUE_SEPARATOR);
        dataBuffer.append(URLEncoder.encode(DESTINATION_METHOD, "UTF-8"));

        URL url = new URL("http://192.168.2.125/VDE/reader.php");
        conn = url.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(dataBuffer.toString());
        wr.flush();
    }


    private String readResult()throws IOException{
        String result = null;
        //Lesen der Rückgabewerte vom Server
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line = null;
        //Solange Daten bereitstehen werden diese gelesen.
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        return sb.toString();
    }

    @Override
    protected void onPostExecute(String result) {
        if(!isBlank(result)) {
            //String[] arr = result.split("\\|");
            this.textView.setText(result);
        }
    }

    private boolean isBlank(String value){
        return value == null || value.trim().isEmpty();
    }


}

PHP脚本看起来像这样

<?php


$reportingLevel = -1; 
error_reporting($reportingLevel); 


checkAuthCode();


$connection = getDBConnection();

function getDBConnection(){

  $dbusername = 'root'; 
  $dbpassword = ''; 
  $dburl='localhost'; 
  $dbname='datenlogger'; 


  $fehler1 = "Fehler 1: Fehler beim aufbauen der Datenbankverbindung!";
    $link = mysqli_connect($dburl, $dbusername, $dbpassword,$dbname);
    if (!$link) {
        die('Verbindung schlug fehl: ' . mysqli_error());
    }

  /* check connection */
  if (mysqli_connect_errno()) {
       die($fehler1);
  }
  return $link;
}


$method =  $_POST['method'];

if ($method == 'allEntrys'){
   getAllEntrys($connection);
}

function getAllEntrys($connection){
  $sqlStmt = "SELECT * FROM temperaturen;";
  $result =  mysqli_query($connection,$sqlStmt);
  $data = array();
  if ($result = $connection->query($sqlStmt)) {
      while ($row = $result->fetch_assoc()) {
        $zeit = $row["zeit"];
        $temp = $row["temp"];
        array_push($data,array("zeit"=> $zeit,"temp"=>$temp));  
      }
  $result->free();
}
  closeConnection($connection);

  foreach ($data as $d){
    echo $d["temp"];
    echo "|";
  }  
}

function checkAuthCode(){
$fehler0 = "Fehler 0: Keine erfolgreiche Authentifizierung!";
if (isset($_POST['authkey']) AND isset($_POST['method'])){
  $authkey = $_POST['authkey'];
  if ($authkey != 'test321'){
    die($authkey);
  }
} else {
  die(var_dump($_POST));
}
}

function closeConnection($connection){
  mysqli_close($connection);
}

?>

0 个答案:

没有答案