2015-02-09 2 views
0

Я хочу получить данные из базы данных и опубликовать их в виде списка. Мой Logcat показывает мне уловку «Исключение 3 поймали». Это означает, что данные, которые могут получить из базы данных, не могут попасть в список. Я также пытался посмотреть, работает ли синтаксический анализ, но на моем логарифме он показывает мне только «----- Вот мои данные -----».Данные анализа данных теряют данные/не анализируют базу данных

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); 
?> 

EDIT

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: [email protected] time:659582292 
+2

Не просто перехватите исключение, распечатайте его и добавьте его в свой вопрос. –

+0

Эти исключения содержат сообщения и трассировки стека по какой-либо причине. – chrylis

+0

отредактировал логарифм. Теперь его версия uptodate с окончательными исключениями catch. Сначала нужно было узнать об этом. .. –

ответ

0

Это может быть странно, копия & Ошибка пасты, но вы попробовали загрузить файл PHP в своем браузере? В случае, если вы получите пустую страницу, это ваша первая ошибка:

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

должно быть:

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

(. Обратите внимание, что "в конце строки)

+0

это был, cla ssic copy & paste failure. –

Смежные вопросы