【问题标题】:How to connect php and wamp in android如何在android中连接php和wamp
【发布时间】:2012-01-02 09:29:47
【问题描述】:

我想在数据库中有一些数据,我的应用程序从那里检索数据。我是初学者,我不知道如何创建我的数据库,尽管我已经编写了 php 脚本和 java 代码来获得这些结果。所以我需要你的帮助来创建那个数据库。我正在使用 WAMP,并且创建了一个数据库。我的问题是这些调用的论点是什么:

mysql_connect("127.0.0.1","root","xxpasswordxx");

还有这个:

public static final String KEY_121 = "http://xx.xx.xxx.xxx/hellomysql/mysqlcon.php"; 

我在某处读到,我必须将 10.0.0.2 和我的目录放在存储我的 php 文件的 C:\wamp\www 下。

编辑:添加代码

这是我的安卓代码:

@SuppressWarnings("null")
private void DoMain() {

    setContentView(R.layout.main);

        // Set the text and call the connect function.  
      //call the method to run the data retreival
        getServerData(KEY_121);
}
    public static final String KEY_121 ="http://10.0.2.2:8888/example.php"; //what in here??

    private String getServerData(String returnString) {

       InputStream is = null;

       String result = "";
        //the year data to send
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("year","1970"));

        //http post
        try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(KEY_121);
                httppost.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());
        }
        //parse json data
        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")
                        );
                        //Get an output to the screen
                        returnString += "\n\t" + jArray.getJSONObject(i); 
                }
        }catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
        }
        return returnString; 
    }    

这是我的 php 文件

<?php

  mysql_connect("127.0.0.1","root","xxpasswordxx"); //what in here??

  mysql_select_db("peopledata");

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

  while($e=mysql_fetch_assoc($q))

          $output[]=$e;

       print(json_encode($output));

mysql_close();

?>

【问题讨论】:

  • 您是否创建了 Web 服务来检索或发送数据?
  • 如果你的意思是,用 Java 和 php 文件编写代码,是的,我做到了。我的问题是我不知道参数的值。

标签: php android database wamp


【解决方案1】:

您需要创建 PHP Web 服务才能从 android 代码访问。在Android端你需要寻找HttpUrlConnection API。

【讨论】:

  • 是的,我已经这样做了(这样想,我已经为该工作创建了一个 ph 文件,并且还在 android 端使用了 HttpUrlConnection API)。我的问题是我不知道将哪些地址作为参数或如何找到它们。
  • 我已经在初始帖子中添加了
  • 你能成功连接到php端的db吗?
  • 我不知道。我的应用程序崩溃了,我在 logcat 上什么也看不到
  • 好吧,在这种情况下,我建议您首先确认您的服务器代码是否工作正常。因为如果你的服务器端有问题,那么检查android代码就没有意义了。
猜你喜欢
  • 2015-12-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-08
  • 2015-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多