douguangxiang0363 2014-04-21 18:17
浏览 74
已采纳

Android上“无法连接到数据库”错误

I want to make an application on Android to view my database from MySQL, but my code doesn't work correctly. The Android device told me that it "Couldn't connect to database".

this is screenshot of my problem : http://s21.postimg.org/fwgx2o5if/aaa.png

This is my MainActivity.java code:

package com.example.bismillahirohamnirohim;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

@SuppressLint("NewApi")
public class MainActivity extends Activity {

    /** Called when the activity is first created. */

    TextView resultView;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        StrictMode.enableDefaults(); // STRICT MODE ENABLED

        resultView = (TextView) findViewById(R.id.result);

        getData();

    }

    public void getData() {

        String result = "";

        InputStream isr = null;

        try {

            HttpClient httpclient = new DefaultHttpClient();

            HttpPost httppost = new HttpPost(
                    "http://localhost/Myfile.php"); // YOUR PHP SCRIPT
                                                                // ADDRESS

            // HttpPost httppost = new
            // HttpPost("http://172.23.193.32/elift-test/myfile.php"); //YOUR
            // PHP SCRIPT ADDRESS

            HttpResponse response = httpclient.execute(httppost);

            HttpEntity entity = response.getEntity();

            isr = entity.getContent();

        }

        catch (Exception e) {

            Log.e("log_tag", "Error in http connection " + e.toString());

            resultView.setText("Couldnt connect to database");

        }

        // convert response to string

        try {

            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    isr, "iso-8859-1"), 8);

            StringBuilder sb = new StringBuilder();

            String line = null;

            while ((line = reader.readLine()) != null) {

                sb.append(line + "
");

            }

            isr.close();

            result = sb.toString();

        }

        catch (Exception e) {

            Log.e("log_tag", "Error  converting result " + e.toString());

        }

        // parse json data

        try {

            String s = "";

            JSONArray jArray = new JSONArray(result);

            for (int i = 0; i < jArray.length(); i++) {

                JSONObject json = jArray.getJSONObject(i);

                s = s + "Name : " + json.getString("id") + " "
                        + json.getString("username");
            }

            resultView.setText(s);

        } catch (Exception e) {

            // TODO: handle exception

            Log.e("log_tag", "Error Parsing Data " + e.toString());

        }

    }

}

This is my PHP code:

<?php

mysql_connect("localhost","root","");

mysql_select_db("jadwal");

$sql1=mysql_query("select * from jadwalkuliah ");

if (!$sql1) {

echo "Could not successfully run query ($sql) from DB: " . mysql_error();

exit;

}

while($row=mysql_fetch_assoc($sql1))

$output[]=$row;


print(json_encode($output));

mysql_close();

?>

I'm using XAMPP.

Please help me. Thank you very much for your help.

  • 写回答

2条回答 默认 最新

  • douhuan1257 2014-04-21 18:36
    关注

    You cannot have localhost in android code. It must be

    http://(system ip address)/Myfile.php
    

    which you get under run>cmd>right click on cmd run as administrator ipconfig

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮