我有此代码,我尝试使用Android Studio从JSON
脚本获取PHP
代码,然后获取图片网址和名称以绘制RecycleView
有一些列表,并填充它...我总是从NullPointerException
JsonArrayRequest
方法接收JSON_DATA_WEB_CALL()
,我的脑袋会爆炸,我找不到解决方案。 PHP
已经过测试,但是它有效,它会正确显示JSON
...任何帮助都会很棒。 Thx提前。这是代码:
访问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);
}
答案 0 :(得分:0)
它已经解决了,这是一个问题,PHP代码返回null答案。无论如何,谢谢。