使用php和JSON将android连接到mySQL

我正在尝试创建一个通过php和JSON连接到mySQL数据库的android应用程序。
THis是我的php 服务器上的文件:</ p>

 &lt;?php 
$ host =“my_host”;
$ username =“my_name”;
$ password =“my_password”;

$ db_name =“my_db_name”;

$ con = mysql_connect(“$ host”,“$ username”,“$ password”)或死亡(“无法连接”);

mysql_select_db(“$ db_name”)或die(“不能选择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);

&GT?;

</ code> </ pre>

这是我的JSON java类代码:</ p>

  public class JSONClass extends AsyncTask&lt; String,Void, 字符串&gt; {

公共HashMap&lt; String,String&gt; tbl = new HashMap&lt; String,String&gt;();
private Context context;

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

@ Override
protected String doInBackground(String ... arg){
尝试{
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())); \ n StringBuffer sb = new StringBuffer(“”);
String line =“”;
while((line = in.readLine())!= null){
sb.append(line);
break;

}
in.close();
返回sb.toString();
} catch(异常e){
返回new String(“Exception:”+ e.getMessage());
}

}
}
</ code> </ pre>

我是新手,只想获得基础和工作 从那里。 php文件应该返回一行作为答案,但据我所知,该函数属于try-catch部分。 我知道结果应该是HashMap。 有人可以告诉我我做错了什么或给我一个提示,以获得结果? 提前谢谢!</ p>
</ div>

展开原文

原文

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!

doushuo2834
doushuo2834 是的我是一行,带有userID-int和显示名称-varchar(254)
6 年多之前 回复
dongluanguang4774
dongluanguang4774 你期望通过运行mysql查询只有一行吗?
6 年多之前 回复

1个回答



这是从 url </ code>获取数据的方法,其中 url </ code>是下面的 PHP脚本的Web地址:</ p>

  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&lt; ja.length(); i ++){
String name = ja.getJSONObject(i).getString(“name”); //写名称列
}
</ code> </ pre>
</ div>

展开原文

原文

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
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐