2012-02-23 118 views
2

我想通过PHP,JSON从Android读取MYSQL数据库中的数据。 我一直在尝试很多不同的例子,但我的Android手机无法读取任何数据。 当我运行加载应用:(如何从Android读取MYSQL数据库?

  • MySQL数据库信息

数据库名称后的应用程序我的手机没有做任何事情:PeopleData 表名:人 表有:ID,姓名,性别,birthyear

  • PHP源:我测试的index.php,结果是正确的

-index.php

<?php 
    mysql_connect("127.0.0.1","root",""); 
    mysql_select_db("PeopleData"); 

    $q=mysql_query("SELECT * FROM people WHERE birthyear>1980"); 
    while($e=mysql_fetch_assoc($q)) 
     $output[]=$e; 
    print(json_encode($output)); 
    mysql_close(); 
    ?> 
  • Android的Java进行读取数据库

    package com.json; 
    
    import java.io.IOException; 
    import org.apache.http.HttpEntity; 
    import org.apache.http.HttpResponse; 
    import org.apache.http.client.ClientProtocolException; 
    import org.apache.http.client.HttpClient; 
    import org.apache.http.client.methods.HttpPost; 
    import org.apache.http.impl.client.DefaultHttpClient; 
    import org.apache.http.util.EntityUtils; 
    import org.json.JSONArray; 
    import org.json.JSONException; 
    import org.json.JSONObject; 
    
    import android.app.Activity; 
    import android.os.AsyncTask; 
    import android.os.Bundle; 
    import android.widget.TextView; 
    import android.widget.Toast; 
    
    public class JSON extends Activity { 
    
    TextView resultView; 
    HttpClient client; 
    JSONObject json; 
    
    // the year data to send 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        // TODO Auto-generated method stub 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.json); 
    
        resultView = (TextView) findViewById(R.id.tvjson); 
        client = new DefaultHttpClient(); 
        new Read().execute("text"); 
    } 
    
    public JSONObject RedData() throws ClientProtocolException,IOException,JSONException { 
    
        HttpPost httppost = new HttpPost("http://127.0.0.1/series/database/index.php");  
        HttpResponse r = client.execute(httppost); 
    
        int status = r.getStatusLine().getStatusCode(); 
    
        if (status == 200) { 
    
         HttpEntity e = r.getEntity(); 
         String data = EntityUtils.toString(e); 
         JSONArray jArray = new JSONArray(data); 
         JSONObject last = jArray.getJSONObject(0); // 0 -> the last object 
         return last; 
    
        } else { 
         Toast.makeText(JSON.this, "error", Toast.LENGTH_LONG); 
         return null; 
        } 
    } 
    
    public class Read extends AsyncTask<String, Integer, String> { 
    
        @Override 
        protected String doInBackground(String... arg0) { 
         // TODO Auto-generated method stub 
         try { 
          json = RedData(); 
    
          return json.getString(arg0[0]); 
         } catch (ClientProtocolException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } catch (JSONException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
         return null; 
        } 
    
        @Override 
        protected void onPostExecute(String data) { 
         // TODO Auto-generated method stub 
         resultView.setText(data); 
        } 
        } 
    
    } 
    
  • xml文件

    <TextView 
        android:id="@+id/tvjson" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" > 
    </TextView> 
    

  • 清单

    <uses-sdk android:minSdkVersion="8" /> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <application 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" > 
        <activity 
         android:name=".JSON" 
         android:label="@string/app_name" > 
         <intent-filter> 
          <action android:name="android.intent.action.MAIN" /> 
    
          <category android:name="android.intent.category.LAUNCHER" /> 
         </intent-filter> 
        </activity> 
    </application> 
    

  • 控制台

    [2012-02-22 16:19:58 - json] Android Launch! [2012-02-22 16:19:58 - json] adb is running normally. [2012-02-22 16:19:58 - json] Performing com.json.JSON activity launch [2012-02-22 16:19:58 - json] Automatic Target Mode: Unable to detect device compatibility. Please select a target device. [2012-02-22 16:19:59 - json] Uploading json.apk onto device '01499EF80D00D009' [2012-02-22 16:19:59 - json] Installing json.apk... [2012-02-22 16:20:01 - json] Success! [2012-02-22 16:20:01 - json] Starting activity com.json.JSON on device 01499EF80D00D009 [2012-02-22 16:20:02 - json] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.json/.JSON }

  • 设备

    doing nothing

我不知道我能做些什么了。请有人帮助我。 谢谢。

+2

IP地址127.0.0.1指的是本地设备,因此对于一个HTTP请求不会离开设备/仿真器。 – 2012-02-23 00:26:20

+2

安全方面,这通常是一个非常糟糕的想法,因为Android和mySQL数据库之间没有保护,您必须将访问凭据公开给移动设备。你通常希望在客户端和数据库之间有一些API层 – 2012-02-23 00:27:01

+0

就像Pekka所说的,与Android的mysql数据库通信的最佳/安全的方法是构建一个API。含义:创建一个Web应用程序/ api,用于控制与数据库的交谈(使用getter和setter,或任何您需要的),然后从Android通过您的web api与数据库通信。 – Shattuck 2012-02-23 00:59:36

回答

0

这应该工作

package com.Online.Test; 

import java.io.IOException; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.util.EntityUtils; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.widget.TextView; 
import android.widget.Toast; 

public class OnlineTestActivity extends Activity { 
    /** Called when the activity is first created. */ 
    TextView resultView; 
    HttpClient client; 
    JSONObject json; 
    String Dat; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     resultView = (TextView) findViewById(R.id.tvjson); 
     client = new DefaultHttpClient(); 
     try { 
      json = RedData(); 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     Dat = json.toString(); 

     new Read().onPostExecute(Dat); 
    } 
    public JSONObject RedData() throws ClientProtocolException, IOException, JSONException { 

     HttpPost httppost = new HttpPost("//link.php");  
     HttpResponse r = client.execute(httppost); 

     // int status = r.getStatusLine().getStatusCode(); 

     //if (status == 200) { 

      HttpEntity e = r.getEntity(); 
      String data = EntityUtils.toString(e); 
      JSONArray jArray = new JSONArray(data); 
      JSONObject last = jArray.getJSONObject(0); // 0 -> the last object 
      return last; 

     // } else { 
     // Toast.makeText(OnlineTestActivity.this, "error", Toast.LENGTH_LONG); 
      // return null; 
     //} 
    } 


    public class Read extends AsyncTask<String, Integer, String> { 

     @Override 
     protected String doInBackground(String... arg0) { 
      // TODO Auto-generated method stub 
      try { 
       json = RedData(); 
       //Toast.makeText(OnlineTestActivity.this, json.getString(arg0[0]), Toast.LENGTH_LONG); 
       return json.getString(arg0[0]); 

      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(String data) { 
      // TODO Auto-generated method stub 
      resultView.setText(data); 
     } 
    } 
} 
+1

如果你解释代码和/或更正。 – 2012-10-26 09:14:56

0

,看一下点

  • IP地址127.0.0.1是指本地设备,因此对于一个 HTTP请求是不留设备/仿真器。
  • 在您的服务器端脚本(在这里PHP),设置 标题('content-type:application/json; charset = utf-8');
  • 在doInBackground的Try块中,检查您的ReadData()null或 不是。
  • 在JSONArray jArray = new JSONArray(data);检查jArray空 或不

是否以下教程可以帮助你

0
  1. 确保您的计算机是系绳在与您的手机相同的网络中进行编辑。

  2. 与您的计算机的ip地址127.0.0.1替换,,让您的计算机的IP地址,执行:在cmd中作为Linux管理员或“使用ifconfig”窗口 “IPCONFIG”,IDK的为Mac

  3. 然后运行你的应用程序,这应该worj ....如果你还没有得到任何数据,替换你的代码,并在这里: 不要使用JSONArray,而是将所有数据从EnyityUtils传递到一个字符串,然后到一个JSONObject不通过JSONArray传递

0
  1. 确保您的计算机与您的手机连接在同一网络中。

  2. 与您的计算机的ip地址127.0.0.1替换,,让您的计算机的IP地址,执行:在cmd中作为Linux管理员或“使用ifconfig”窗口 “IPCONFIG”,IDK的为Mac

  3. 然后运行你的应用程序,这应该让你担心..如果你仍然没有得到任何数据,替换你的代码,并且特别在这里: 不要使用JSONArray,而是将所有数据从EnyityUtils传递到String,然后传递给JSONObject而不通过JSONArray