2017-02-27 48 views
0

我有這個代碼,我試圖從PHP腳本獲得JSON代碼與Android Studio,並在此之後,獲取圖像的URL和名稱繪製RecycleView一些列表和填充它...我總是從方法接收NullPointerException方法,我的頭會爆炸,我沒有找到解決方案。 PHP已經過測試,它的工作原理,它正確顯示JSON ...任何幫助將是偉大的。 Thx提前。下面是代碼:填充Recycleview在JsonArrayRequest中給出null

訪問getdata.php

<?php 

    //importing Config file 
    include 'include/Config.php'; 

    // Create connection 
    $conn = new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_DATABASE); 

    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
    } 

    $sql ="SELECT * FROM imageplums"; 
    $result = $conn->query($sql); 

    if ($result->num_rows >0) { 
     // output data of each row 
     while($row[] = $result->fetch_assoc()) { 
      $tem = $row; 
      $json = json_encode($tem); 
     } 
    } else { 
     echo "0 results"; 
    } 

    echo $json; 
    $conn->close(); 
?> 

MainActivity.Java

public class MainActivity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 
    public TextView text; 
    public static String MAIL_KEY; 
    RecyclerView recyclerView; 
    RecyclerView.Adapter pAdapter; 
    RecyclerView.LayoutManager recyclerViewlayoutManager; 
    List<PlumImage> plumList; 

    String GET_JSON_DATA_HTTP_URL = "http://192.168.1.39:8080/plumster/getdata.php"; 
    String JSON_IMAGE_TITLE_NAME = "image_title"; 
    String JSON_IMAGE_URL = "image_url"; 

    JsonArrayRequest jsonArrayRequest ; 
    RequestQueue requestQueue ; 

    private SessionManager session; 
    private SQLiteHandler db; 


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

    plumList=new ArrayList<>(); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      addPlum(); 
     } 
    }); 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); //Creamos Navigationview que corresponde a nav_header_main.xml 
    navigationView.setNavigationItemSelectedListener(this); 
    View header=navigationView.getHeaderView(0); 
    text=(TextView)header.findViewById(R.id.textViewEmail); 

    // SqLite database handler 
    db = new SQLiteHandler(getApplicationContext()); 

    // session manager 
    session = new SessionManager(getApplicationContext()); 


    // Fetching user details from sqlite 
    HashMap<String, String> user = db.getUserDetails(); 

    MAIL_KEY = user.get("email"); 



    recyclerView=(RecyclerView) findViewById(R.id.recyclerview); 
    recyclerView.setHasFixedSize(true); 
    recyclerViewlayoutManager = new LinearLayoutManager(this); 

    recyclerView.setLayoutManager(recyclerViewlayoutManager); 
    //recyclerView.setLayoutManager(new GridLayoutManager(this,2)); 
    JSON_DATA_WEB_CALL(); 

} 



public void JSON_DATA_WEB_CALL() { 
    //Showing a progress dialog 
    //final ProgressDialog loading = ProgressDialog.show(this, "Loading data", "Please wait...", false, false); 

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL, 

      new Response.Listener<JSONArray>() { 
       @Override 
       public void onResponse(JSONArray response) { 

        JSON_PARSE_DATA_AFTER_WEBCALL(response); 
        //loading.dismiss(); 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 

        Toast.makeText(getApplicationContext(), 
          error.networkResponse.toString(), Toast.LENGTH_LONG) 
          .show(); 
       } 
      }); 

    requestQueue = Volley.newRequestQueue(this); 

    requestQueue.add(jsonArrayRequest); 
} 

//This method will parse json data 
public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){ 

    for(int i = 0; i<array.length(); i++) { 

     PlumImage plumImage = new PlumImage(); 

     JSONObject json = null; 
     try { 

      json = array.getJSONObject(i); 

      plumImage.setName(json.getString(JSON_IMAGE_TITLE_NAME)); 

      plumImage.setPhoto(json.getString(JSON_IMAGE_URL)); 

     } catch (JSONException e) { 

      e.printStackTrace(); 
     } 
     plumList.add(plumImage); 
    } 

    pAdapter = new PlumAdapter(plumList, this); 

    recyclerView.setAdapter(pAdapter); 
} 
+1

你能指定你在哪裏得到NPE的代碼嗎? –

+0

在JSON DATA WEB CALL方法中,當我創建jsonarrayrequest變量時,它總是返回null,並且我有兩個記錄un MySQL Db,所以它不應該爲空... – Lopiv2

+0

請** [編輯] **您的問題併發布堆棧跟蹤。 – Pang

回答

0

它已經解決了,這是有其返回null回答PHP代碼中的問題。無論如何,謝謝。

+0

很高興你能工作。順便提一下,請提供儘可能多的信息。你從未指定過你獲得NPE的確切位置。你提到了一個方法,但是一個堆棧跟蹤會告訴你確切的位置。因爲你提到的方法是19行代碼,所以我們需要知道這一點。我也建議檢查Retrofit2。我覺得比Volley更好。 –