【问题标题】:how to connect android to mysql?如何将android连接到mysql?
【发布时间】:2014-05-20 03:42:06
【问题描述】:

请帮助我,我正在尝试通过 php 和 json 数组中的连接将我的 android 应用程序连接到 localhost 中的 mysql 数据库,但我无法弄清楚问题是什么,我无法查看数据库中的数据。

这是我的文件。

Connection.php

<?php 

$db_con = mysqli_connect('localhost', 'root', '', 'android') or die ("connection error");;

$query = "SELECT * FROM product";

$results = mysqli_query($db_con, $query) or die ("query error");;

while($row = mysqli_fetch_assoc($results)){


    $output[]=$row;
}
echo json_encode($output);
?>

这就是android java "MainActivity.java"

public class MainActivity extends ActionBarActivity {

    TextView viewItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

        viewItem = (TextView) findViewById(R.id.itemView);
        // Button btnViewItems = (Button) findViewById(R.id.btnViewItems);

        getData();
    }

    public void getData() {

        String results = "";
        InputStream isr = null;

        // Http post
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://10.0.2.2:8080/android/Connection.php");
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
        } catch (Exception e) {
            Log.e("log-tag", "Error in http connection" + e.toString());
            viewItem.setText("Cannot connect to database");

        }

        // Converting 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 + "\n");
            }
            isr.close();
            results = sb.toString();
        } catch (Exception e) {
            Log.e("log-tag", "Error Converting string" + e.toString());
            viewItem.setText("Cannot convert string");

        }

        // prase jason data
        try {
            String s = "";
            JSONArray jArray = new JSONArray(results);

            for (int i = 0; i < jArray.length(); i++) {
                JSONObject jOb = jArray.getJSONObject(i);
                s = s + jOb.getString("id") + " || " + jOb.getString("name")
                        + " || " + jOb.getString("cost") + "\n\n";

            }

            viewItem.setText(s);

        } catch (Exception e) {
            Log.e("log-tag", "prasing json data" + e.toString());
            viewItem.setText("cannot prase json data");

        }
    }
}

外面一直在说:“无法使用 json 数据”

这是日志错误:

E/log-tag(1904): Error in http connectionandroid.os.NetworkOnMainThreadException
E/log-tag(1904): Error Converting stringjava.lang.NullPointerException: lock == null
E/log-tag(1904): prasing json dataorg.json.JSONException: End of input at character 0 of 

谢谢

【问题讨论】:

  • 异常说明了什么?
  • 发布有关您遇到的异常的问题时,始终包含完整的异常详细信息。目前您正在捕获该异常,记录是,而不是告诉我们它说了什么,有效地挫败了平台试图为您提供的所有帮助。
  • 请同时包含来自 logcat 的日志 :)
  • 对不起,这是异常错误:05-19 23:34:30.525:E/log-tag(1904):http 连接出错android.os.NetworkOnMainThreadException 05-19 23:34: 30.525:E/log-tag(1904):转换字符串时出错。 字符 0 处的输入
  • 请在此处打印您的 json reposnce..以便我们为您提供帮助//

标签: java php android mysql json


【解决方案1】:

您需要在不同的线程而不是主线程上运行 HTTP 请求。 Android API 不允许在主线程上运行 HTTP 请求。尝试使用 Runnable() 在单独的线程上异步运行 http 请求。

【讨论】:

  • 感谢您的回复,但我不认为这是问题,就像您参考我刚刚包含的日志错误一样,您会发现它是从 HTTP 连接开始的。所以我猜它与数据库有关
  • 是的,我明白了。我猜你的问题是试图在主线程上运行 HTTP 请求。 Android 改变了这个方案。现在是不允许的。因为它不是异步的。您需要使用 Runnable() 在自己的线程中执行 http 请求,而不是占用主线程。
  • 感谢您的回复,但您能帮我写这段代码吗?我不太熟悉它。再次感谢
  • [非阻塞模式下的 HTTP 请求](androidsnippets.com/non-blocking-web-request)。这里它演示了如何在非阻塞模式下运行 HTTP 请求,即异步。希望对您有所帮助。
猜你喜欢
  • 1970-01-01
  • 2019-01-18
  • 2020-04-09
  • 2017-08-11
  • 2017-12-07
  • 2019-08-16
  • 1970-01-01
  • 1970-01-01
  • 2014-06-11
相关资源
最近更新 更多