我有這個代碼,我試圖從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);
}
你能指定你在哪裏得到NPE的代碼嗎? –
在JSON DATA WEB CALL方法中,當我創建jsonarrayrequest變量時,它總是返回null,並且我有兩個記錄un MySQL Db,所以它不應該爲空... – Lopiv2
請** [編輯] **您的問題併發布堆棧跟蹤。 – Pang