我的MainActivity.java类是:
错误是:
java.lang.NoClassDefFoundError:android.volley.toolbox.StringRequest
getData()和onClick()中的错误
我添加了凌空库。在运行应用程序时,按钮点击后很遗憾应用程序已关闭..数据库连接失败。
private void getData() {
String id = id1.getText().toString().trim();
if (id.equals("")) {
Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show();
return;
}
loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
String url = Config.DATA_URL+id1.getText().toString().trim();
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response){
String name="";
String age="";
String address="";
String district= "";
String state="";
String block="";
String panchayat="";
String phone="";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
JSONObject farmerData = result.getJSONObject(0);
name = farmerData.getString(Config.KEY_NAME);
age=farmerData.getString(Config.KEY_AGE);
address = farmerData.getString(Config.KEY_ADDRESS);
district = farmerData.getString(Config.KEY_DISTRICT);
state = farmerData.getString(Config.KEY_STATE);
panchayat = farmerData.getString(Config.KEY_PANCHAYAT);
block = farmerData.getString(Config.KEY_BLOCK);
phone = farmerData.getString(Config.KEY_PHONE);
} catch (JSONException e) {
e.printStackTrace();
}
textView1.setText("Name:\t"+name+"\nAge:\t"+age+"\nAddress:\t" +address+ "\nDistrict:\t"+district+"\nState:\t"+state+"\nPanchayat:\t"+panchayat+"\nBlock:\t"+block+"\nPhone:\t"+phone);
}
@Override
public void onClick(View v) {
getData();
}