douhu1990 2014-05-27 18:42
浏览 78

使用php和JSON将android连接到mySQL

I'm trying to create an android application that connects to mySQL database through php and JSON. THis is my php file on the server:

<?php
$host="my_host";
$username="my_name";
$password="my_password";
$db_name="my_db_name";

$con=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
$sql = "SELECT    UserID, DisplayName
FROM      User
WHERE     (UserName LIKE '%it%') OR (DisplayName LIKE '%it%')"; 
$result = mysql_query($sql);
$json = array();

if(mysql_num_rows($result)){
while($row=mysql_fetch_assoc($result)){
$json['UserRes'][]=$row;
}
}
mysql_close($con);
echo json_encode($json); 
?> 

And this is my JSON java class code:

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

public HashMap<String, String> tbl = new HashMap<String, String>();
private Context context;

public JSONClass(Context context) {
    this.context = context;
}

@Override
protected String doInBackground(String... arg0) {
    try{
        String link = "some url";
        URL url = new URL(link);
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI(link));
        HttpResponse response = client.execute(request);
        BufferedReader in = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String line="";
        while ((line = in.readLine()) != null) {
            sb.append(line);
            break;
        }
        in.close();
        return sb.toString();
    }catch(Exception e){
        return new String("Exception: " + e.getMessage());
    }
}
}

I'm new with this and just want to get the basics and work from there. The php file should return one row as an answer, but as far as I can figure it out, the function falls in the try - catch part. I know that the result should come as HashMap. Can someone please tell me what I'm doing wrong or give me a tip to get the result? Thank you in advance!

  • 写回答

1条回答 默认 最新

  • doujingya1166 2014-05-27 22:55
    关注

    This is how you fetch data from url, where url below is the web address of your PHP script:

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);
    post.addHeader("Content-Type", "application/x-www-form-urlencoded");
    HttpResponse response = client.execute(post);
    HttpEntity entity = response.getEntity();
    String result = EntityUtils.toString(entity, "utf-8");
    JSONArray ja = new JSONArray(result);
    for(int i = 0 ; i < ja.length() ; i++){
      String name = ja.getJSONObject(i).getString("name"); //write name of column
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决