安卓连接mysql

标签 android mysql json

我正在尝试在 android 应用程序中连接 mysql。下面是我的代码。运行代码后,我得到“错误解析数据 org.json.JSONException:输入结束于字符 0 of”错误。我用的是 this教程

代码

public class Test extends Activity{
    /** Called when the activity is first created. */
     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.mainabout);
            String result = "";
          //the year data to send
          ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
          nameValuePairs.add(new BasicNameValuePair("year","1980"));
          InputStream is = null;               
          //http post
          try{
                  HttpClient httpclient = new DefaultHttpClient();

                  HttpGet httppost = new HttpGet("http://www.pherma.net84.net/admin/getAllPeopleBornAfter.php");
                 // httppost.s//setEntity(new UrlEncodedFormEntity(nameValuePairs));
                  HttpResponse response = httpclient.execute(httppost);
                  HttpEntity entity = response.getEntity();
                   is = entity.getContent();
          }catch(Exception e){
                  Log.e("log_tag", "Error in http connection "+e.toString());
          }
          //convert response to string
          try{
                  BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                  StringBuilder sb = new StringBuilder();
                  String line = null;
                  while ((line = reader.readLine()) != null) {
                          sb.append(line + "\n");
                  }
                  is.close();

                  result=sb.toString();
          }
          catch(Exception e){
                  Log.e("log_tag", "Error converting result "+e.toString());
          }

          try
          {
              JSONArray jArray = new JSONArray(result);
              for(int i=0;i<jArray.length();i++){
                      JSONObject json_data = jArray.getJSONObject(i);
                      Log.i("log_tag","id: "+json_data.getInt("id")+
                              ", name: "+json_data.getString("name")+
                              ", sex: "+json_data.getInt("sex")+
                              ", birthyear: "+json_data.getInt("birthyear")
                      );
              }

          }
          catch(JSONException e){
              Log.e("log_tag", "Error parsing data "+e.toString());
          } 
        }
    }

日志 enter image description here

php

<?php
mysql_connect(".com","a4055820_root","");
mysql_select_db("a4055820_pherma");

$q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");
while($e=mysql_fetch_assoc($q))
        $output[]=$e;

print(json_encode($output));

mysql_close();
?>

最佳答案

问题是我忘记在 list 中授予互联网访问权限。

关于安卓连接mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10667128/

相关文章:

Android 平台上的 Android 库和 Java 经典库(.jar)

android - 来自谷歌开发人员的交互式图表示例显示错误 eclipse

Python 无法解码 json 文件,尽管它看起来有效

java - E/RecyclerView : No adapter attached; skipping layout -- when making a todo list app

android - 如何在 Android 应用程序中启用 TLS 1.2 支持(在 Android 4.1 JB 上运行)

php - 将 MySQL 数据加载到 PHP

php - 如何为数组中的每个元素调用相同的函数?

java - 从 Java 将 'yyyy-mm-dd' 字符串转换为 MySQL 的日期类型变量

android - Flutter 在滚动时加载 api 数据

javascript - 从多个对象创建有效的 JSON