2017-07-27 18 views
-1

データベースからデータを取得しようとしていますが、アクティビティをアクティブにすると空の画面に戻ります。私は2つの例外を参照してください。私はこの問題を取得していますなぜリストビューでデータベースからのデータが表示されない

enter image description here

私は知りませんが、すべてが正常に思える

これは私のShowUsers活動です:

package ie.example.artur.adminapp; 

import android.content.Intent; 
import android.os.Bundle; 
import android.os.StrictMode; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toolbar; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 

import android.view.View; 

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

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.List; 

import static android.R.attr.name; 

/** 
* Created by family on 24/07/2017. 
*/ 

public class ShowUsers extends AppCompatActivity { 


    ListView lv; 
    //String[] names = {"Amy","John","Joseph","Carl"}; 
    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.showusers); 

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

     //Inlfate the list view with the items 

     //lv.setAdapter(new ArrayAdapter<String>(ShowUsers.this,android.R.layout.simple_list_item_1,arr)); 

     android.widget.Toolbar toolbar = (android.widget.Toolbar) findViewById(R.id.toolbar); 

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

      HttpPost httpPost = new HttpPost("http://10.3.2.51/www/"); 

      HttpResponse response = httpClient.execute(httpPost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 
      //SETUP THE INPUTSTREAM TO RECEIVE THE DATA (INITIAL) 
     }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 

      result = sb.toString(); 
      is.close(); 
      //check the data by printing the results in the logcat 

      System.out.println("-----Here is my data -----"); 
      System.out.println(result); 

     }catch(Exception e){ 
      System.out.print("Exception 2 caught"); 
     } 

     try{ 

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


      for(int i=0; i<count; i++){ 
       //create a json object to extract the data 
       JSONObject json_data = jsonArray.getJSONObject(i); 
       temp +=json_data.getString("name")+":"; 
       //where name is attribute of the getdata table 
       //using ':' as the delimiter 

      } 

      //Afer receiving everything store the contents in a string array from temo separated using the delimiter 
      arr = temp.split(":"); 
      //set the list adapter with the array arr 

      lv.setAdapter(new ArrayAdapter<String>(ShowUsers.this,android.R.layout.simple_list_item_1, arr)); 

     }catch(Exception e){ 
      System.out.println("I am really bored of writing all these exception blocks"); 
     } 



    } 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     { 
      switch (item.getItemId()) 
      { 
       case R.id.action_settings : startActivity (new Intent(this, ShowUsers.class)); 
        break; 
      } 
      return super.onOptionsItemSelected(item); 
     }} 




} 

これはアプリにデータベースを接続しtut.phpです:

<?php 

$con=mysql_connect("localhost","root",""); 
mysql_select_db("socialmedia_website",$con); 

$r=mysqli_query("select name from users where 1)"; 

while($row=mysql_fetch_array($r)) 

    { 

     $out[]=$row; 
    } 

    print(json_encode($out)); 
    mysql_close($con) 

のフルスタックトレース:

07-27 15:50:18.152 10602-10602/ie.example.artur.adminapp I/art: Late-enabling -Xcheck:jni 
07-27 15:50:18.152 10602-10602/ie.example.artur.adminapp I/art: VMHOOK: rlim_cur : 0 pid:10602 
07-27 15:50:18.233 10602-10602/ie.example.artur.adminapp W/System: ClassLoader referenced unknown path: /data/app/ie.example.artur.adminapp-1/lib/arm 
07-27 15:50:18.324 10602-10602/ie.example.artur.adminapp W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 
07-27 15:50:18.821 10602-10639/ie.example.artur.adminapp I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: Nondeterministic_AU_msm8226_LA.BF.1.1.3_RB1__release_AU (I8c58819290) 
                     OpenGL ES Shader Compiler Version: E031.29.00.00 
                     Build Date: 01/24/16 Sun 
                     Local Branch: 
                     Remote Branch: quic/LA.BF.1.1.3_rb1.9 
                     Local Patches: NONE 
                     Reconstruct Branch: NOTHING 
07-27 15:50:19.331 10602-10602/ie.example.artur.adminapp I/InputMethodManager: [startInputInner] EditorInfo { packageName=ie.example.artur.adminapp, inputType=0x20001, imeOptions=0x44000006, privateImeOptions=null }, [email protected], mServedView=android.support.v7.widget.AppCompatEditText{a91c21f VFED..CL. .F....I. 32,318-232,406 #7f0c006f app:id/editTextPassword}, mServedInputConnectionWrapper=android.view.[email protected]fb5710f 
07-27 15:50:26.856 10602-10602/ie.example.artur.adminapp I/ListPopupWindow: Could not find method setEpicenterBounds(Rect) on PopupWindow. Oh well. 
07-27 15:50:26.886 10602-10602/ie.example.artur.adminapp W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView 
07-27 15:51:33.375 10602-10602/ie.example.artur.adminapp I/System.out: Exception 1 caught 
07-27 15:51:33.394 10602-10602/ie.example.artur.adminapp I/System.out: Exception 2 caughtI am really bored of writing all these exception blocks 
07-27 15:51:33.400 10602-10602/ie.example.artur.adminapp I/Choreographer: Skipped 3794 frames! The application may be doing too much work on its main thread. 
07-27 15:54:58.497 10602-10609/ie.example.artur.adminapp W/art: Suspending all threads took: 11.806ms 
+0

これらの例外の完全なスタックトレースを教えていただけますか? – ChrisStillwell

+0

@ChrisStillwellコードを更新しました。見てください – Lucy

+0

実際の例外をキャッチブロックに印刷して、スタックトレースが実際にはあまり役に立たないようにしてください。あなたの例外で 'printStackTrace()'を呼び出して完全なスタックトレースを得ることができます。 – ChrisStillwell

答えて

0

は、あなたがインターネットのアクセス権を持っていますか?そうでない場合は、this answerに記載されているマニフェストに追加します。

<manifest xlmns:android...> 
... 
<uses-permission android:name="android.permission.INTERNET" /> 
<application ... 
</manifest> 

これが解決しない場合は、スタックトレースを印刷してみてください。あなたのキャッチ方式ではどこでも次の行を追加します。

e.printStackTrace(); 

のApache HTTPクライアントは、現在ところ​​で推奨されていません。 OkHttpまたはHttpURLConnectionクラスを使用することをお勧めします。

StrictModeを無効にすることも悪いことです。

関連する問題