Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从Android连接到PHP中的数据库_Php_Android_Mysql_Database - Fatal编程技术网

无法从Android连接到PHP中的数据库

无法从Android连接到PHP中的数据库,php,android,mysql,database,Php,Android,Mysql,Database,我无法用PHP连接到数据库。 我的Android代码是: private void connecttodb() { String result = ""; InputStream isr = null; try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicName

我无法用PHP连接到数据库。 我的Android代码是:

private void connecttodb() {
    String result = "";
    InputStream isr = null;
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("username", etusername.getText().toString().trim()));
        nameValuePairs.add(new BasicNameValuePair("password", etpassword.getText().toString().trim()));
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://jcprivatesite.uboxi.com/teenpatti/insert.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        isr = entity.getContent();
    } catch (Exception e) {
        Log.e("mylog", "Error in http connection " + e.toString());
    }
}
我的PHP代码是:

:

我真的尝试了很多代码,但都没有用。一件奇怪的事情是Android正在执行linkurl,但它没有发布。我的意思是,当我将代码替换为:

:

我的数据库正在更新。但当我放置post方法或调用$\u post['username']时,它并没有更新。
请帮帮我。如果您能提供示例代码,我将不胜感激。

Android是一个单线程应用程序,每当您执行网络任务(如从url获取JSON)时,您需要通过AsyncTask来完成,它很可能就是为了完成这些任务而创建的

 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }

有关AsyncTask的更多信息,请按照说明进行操作。

它在AsyncTask中吗?不,我刚刚创建了一个新线程并调用了它…请访问下面的url,它将对您有所帮助。与上述相同的url相比,您的php代码的url在您的代码中有所不同。确实如此。http://jcprivatesite.uboxi.com/teenpatti/insert.php 请发布insert.php的代码。为什么在这里显示PHPScript文件名为php1.txt和php2.txt?它应该是php1.php和php2.php,因为扩展名应该是.php。
<?php
error_reporting(0);
require_once('databaseClass.php');
$username = 'username';
$password = 'password';
$query="INSERT INTO `u624768762_teenp`.`login` (`username`, `password`) VALUES ($username, $password);";
$result=$link1->query($query);
while($row=mysql_fetch_assoc($result))
    $output[]=$row;
print(json_encode($output));
mysqli_close($con);
?>
 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }
new DownloadFilesTask().execute(url1, url2, url3);