解析数据丢失数据/不解析数据库

时间:2015-02-09 23:52:07

标签: java php android database parsing

我想从数据库中获取数据并将其发布到listview中。我的Logcat向我展示了“捕获异常3”的问题。这意味着,可能从数据库中获取的数据无法进入列表视图。我也试着看看解析是否有效,但在我的logcat上它只显示“-----这是我的数据-----”。

android.java

public class MainActivity extends ActionBarActivity {


ListView lv;
InputStream is = null;
String line = null;
String result = null;
String temp = "";
String[] arr;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lv = (ListView) findViewById(R.id.lv);


    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    /**
     * Set up the code to fetch data from the database
     */

    try{
        HttpClient httpClient = new DefaultHttpClient();
        /**
         * Specify the url and the name of the php file that we are going to use as a parameter to the HttpPost method
         */

        HttpPost httpPost = new HttpPost("http://URLURLURLURL/parse.php");
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity  entity = response.getEntity();
        //Setup the Inputstream to receice the data (initial)
        is = entity.getContent();

    }catch (Exception e){
        System.out.println("Exception 1 caught");
    }

    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"),8);
        /**
         * Create a string builder object to hold the data
         */

        StringBuilder sb = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        /**
         * Use the toString() method to get the data in the result
         *
         */
        is.close();
        result = sb.toString();

        /**
         * Let` check the data by printing the result in the logcat
         */
        System.out.println("________________ Here is my data_______________");
        System.out.println(result);

    }catch(Exception e){
        System.out.println("Exception 2 caught");
    }
    /**
     * result now contains the data in the form of json.
     * let`s inflate it in the form of the list
     */


    try{
        JSONArray jArray = new JSONArray(result); //Create a json array
        int count = jArray.length();

        for(int i=0; i<count; i++){
            //Create a json object to extract the data
            JSONObject json_data = jArray.getJSONObject(i);
            temp += json_data.getString("names")+":";
            /**
             * Where names is the attribute of the getdata table
             * I am using ":" as the delimiter
             */

        }
        //After receiving everything store the contents in a string array from temp separated using the delimiter
        arr = temp.split(":");
        /**
         * Set the list adapter with the array arr
         */
        lv.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, arr));

    }catch(Exception e){
        System.out.println("Exception 3 caught");
    }
    }

}

parse.php

    <?php
$con=mysql_connect("localhost:3306","root","root");
mysql_select_db("tutorial",$con); 

$r=mysql_query("select names from getdata where 1“);

while($row=mysql_fetch_array($r))
{
$out[]=$row;
}
print(json_encode($out));
mysql_close($con);
?>

修改

logcat的

    02-10 02:49:03.020  31823-31823/de.johndoe.myapplication I/System.out: ________________ Here is my data_______________
02-10 02:49:03.020  31823-31823/de.johndoe.myapplication W/System.err: org.json.JSONException: End of input at character 0 of
02-10 02:49:03.025  31823-31823/de.johndoe.myapplication W/System.err: at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
02-10 02:49:03.025  31823-31823/de.johndoe.myapplication W/System.err: at org.json.JSONTokener.nextValue(JSONTokener.java:97)
02-10 02:49:03.025  31823-31823/de.johndoe.myapplication W/System.err: at org.json.JSONArray.<init>(JSONArray.java:92)
02-10 02:49:03.025  31823-31823/de.johndoe.myapplication W/System.err: at org.json.JSONArray.<init>(JSONArray.java:108)
02-10 02:49:03.025  31823-31823/de.johndoe.myapplication W/System.err: at de.johndoe.myapplication.MainActivity.onCreate(MainActivity.java:107)
02-10 02:49:03.025  31823-31823/de.johndoe.myapplication W/System.err: at android.app.Activity.performCreate(Activity.java:5231)
02-10 02:49:03.025  31823-31823/de.johndoe.myapplication W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-10 02:49:03.030  31823-31823/de.johndoe.myapplication W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
02-10 02:49:03.030  31823-31823/de.johndoe.myapplication W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2248)
02-10 02:49:03.030  31823-31823/de.johndoe.myapplication W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:138)
02-10 02:49:03.030  31823-31823/de.johndoe.myapplication W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
02-10 02:49:03.030  31823-31823/de.johndoe.myapplication W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
02-10 02:49:03.030  31823-31823/de.johndoe.myapplication W/System.err: at android.os.Looper.loop(Looper.java:136)
02-10 02:49:03.030  31823-31823/de.johndoe.myapplication W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5050)
02-10 02:49:03.030  31823-31823/de.johndoe.myapplication W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
02-10 02:49:03.030  31823-31823/de.johndoe.myapplication W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
02-10 02:49:03.030  31823-31823/de.johndoe.myapplication W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-10 02:49:03.030  31823-31823/de.johndoe.myapplication W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-10 02:49:03.030  31823-31823/de.johndoe.myapplication W/System.err: at dalvik.system.NativeStart.main(Native Method)
02-10 02:49:03.030  31823-31823/de.johndoe.myapplication I/System.out: Exception 3 caught
02-10 02:49:03.180  31823-31823/de.johndoe.myapplication D/libEGL: loaded /system/lib/egl/libEGL_mali.so
02-10 02:49:03.190  31823-31823/de.johndoe.myapplication D/libEGL: loaded /system/lib/egl/libGLESv1_CM_mali.so
02-10 02:49:03.200  31823-31823/de.johndoe.myapplication D/libEGL: loaded /system/lib/egl/libGLESv2_mali.so
02-10 02:49:03.215  31823-31823/de.johndoe.myapplication E/: Device driver API match
    Device driver API version: 23
    User space API version: 23
02-10 02:49:03.215  31823-31823/de.johndoe.myapplication E/: mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Wed Oct  9 21:05:57 KST 2013
02-10 02:49:03.275  31823-31823/de.johndoe.myapplication D/OpenGLRenderer: Enabling debug mode 0
02-10 02:49:03.430  31823-31823/de.johndoe.myapplication I/ActivityManager: Timeline: Activity_idle id: android.os.BinderProxy@41d18418 time:659582292

1 个答案:

答案 0 :(得分:0)

这可能是一个奇怪的副本&amp;粘贴失败,但您是否尝试在浏览器中加载PHP文件? 如果你得到一个空白页面,这是你的第一个错误:

$r=mysql_query("select names from getdata where 1“);

应该是:

$r=mysql_query("select names from getdata where 1");

(注意“在行尾。”