简体   繁体   English

使用php和JSON将android连接到mySQL

[英]Connecting android to mySQL using php and JSON

I'm trying to create an android application that connects to mySQL database through php and JSON. 我正在尝试创建一个通过php和JSON连接到mySQL数据库的android应用程序。 THis is my php file on the server: 这是我在服务器上的php文件:

<?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: 这是我的JSON Java类代码:

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. php文件应该返回一行作为答案,但是据我所知,该函数属于try-catch部分。 I know that the result should come as HashMap. 我知道结果应该是HashMap。 Can someone please tell me what I'm doing wrong or give me a tip to get the result? 有人可以告诉我我做错了什么,还是给我小费以得到结果? Thank you in advance! 先感谢您!

This is how you fetch data from url , where url below is the web address of your PHP script: 这是您从url获取数据的方式,其中以下url是您的PHP脚本的网址:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM