如果JSON为空,Android应用程序将崩溃
我正在开发一个JSON对象的应用程序,它将数据返回到ListView。 如果json有数据,则在listview中显示数据..如果没有日期,则应用程序崩溃。当json为空时,我的应用程序崩溃。如何解决此问题 这是android代码如果JSON为空,Android应用程序将崩溃,android,json,listview,Android,Json,Listview,我正在开发一个JSON对象的应用程序,它将数据返回到ListView。 如果json有数据,则在listview中显示数据..如果没有日期,则应用程序崩溃。当json为空时,我的应用程序崩溃。如何解决此问题 这是android代码 public class EmployeePaymentHistory extends Fragment { HttpParse httpParse = new HttpParse(); ProgressDialog pDialog; List
public class EmployeePaymentHistory extends Fragment {
HttpParse httpParse = new HttpParse();
ProgressDialog pDialog;
ListView CategoryListView;
ProgressBar progressBar;
List<String> IdList = new ArrayList<>();
private String TAG = EmployeePaymentHistory.class.getSimpleName();
// Http Url For Filter Student Data from Id Sent from previous activity.
String finalResult ;
HashMap<String,String> hashMap = new HashMap<>();
String ParseResult ;
HashMap<String,String> ResultHash = new HashMap<>();
String FinalJSonObject ;
String TempItem;
ProgressDialog progressDialog2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_employee_payment, container, false);
CategoryListView = (ListView)view.findViewById(R.id.listview1);
progressBar = (ProgressBar)view.findViewById(R.id.progressBar);
//Receiving the ListView Clicked item value send by previous activity.
TempItem = getActivity().getIntent().getExtras().getString("ListViewValue1");
//Calling method to filter Student Record and open selected record.
HttpWebCall(TempItem);
// Add Click listener on Delete button.
return view;
}
// Method to Delete Student Record
//Method to show current record Current Selected Record
public void HttpWebCall(final String PreviousListViewClickedItem){
class HttpWebCallFunction extends AsyncTask<String,Void,String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = ProgressDialog.show(getActivity(),"Loading Data",null,true,true);
}
@Override
protected void onPostExecute(String httpResponseMsg) {
super.onPostExecute(httpResponseMsg);
pDialog.dismiss();
//Storing Complete JSon Object into String Variable.
FinalJSonObject = httpResponseMsg ;
//Parsing the Stored JSOn String to GetHttpResponse Method.
new EmployeePaymentHistory.GetHttpResponse(getActivity()).execute();
}
@Override
protected String doInBackground(String... params) {
ResultHash.put("CustomerID",params[0]);
ParseResult = httpParse.postRequest(ResultHash, api.EmployeePayment);
return ParseResult;
}
}
HttpWebCallFunction httpWebCallFunction = new HttpWebCallFunction();
httpWebCallFunction.execute(PreviousListViewClickedItem);
}
// Parsing Complete JSON Object.
private class GetHttpResponse extends AsyncTask<Void, Void, Void>
{
public Context context;
List<Customer> CategoryList;
public GetHttpResponse(Context context)
{
this.context = context;
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0)
{
try
{
if(FinalJSonObject != null)
{
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(FinalJSonObject);
JSONObject jsonObject;
Customer category;
CategoryList = new ArrayList<Customer>();
for(int i=0; i<jsonArray.length(); i++)
{
category = new Customer();
jsonObject = jsonArray.getJSONObject(i);
category.CustomerName = jsonObject.getString("date").toString();
category.Customertotal = jsonObject.getString("account").toString();
category.CustomerPaid = jsonObject.getString("total").toString();
category.CustomerUnPaid = jsonObject.getString("status").toString();
CategoryList.add(category);
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
else
{
Toast.makeText(context, "abcc", Toast.LENGTH_SHORT).show();
}
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
progressBar.setVisibility(View.GONE);
CategoryListView.setVisibility(View.VISIBLE);
CustomerListAdapterClass adapter = new CustomerListAdapterClass(CategoryList, context);
CategoryListView.setAdapter(adapter);
}
}
}
公共类EmployeePaymentHistory扩展片段{
HttpParse HttpParse=新的HttpParse();
ProgressDialog;
ListView类别列表视图;
ProgressBar ProgressBar;
List IdList=new ArrayList();
私有字符串标记=EmployeePaymentHistory.class.getSimpleName();
//从上一活动发送的Id筛选学生数据的Http Url。
字符串最终结果;
HashMap HashMap=新的HashMap();
字符串解析结果;
HashMap ResultHash=新的HashMap();
字符串FinalJSonObject;
字符串项;
进行对话2;
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图=充气机。充气(R.layout.activity\u employee\u payment,container,false);
CategoryListView=(ListView)view.findViewById(R.id.listview1);
progressBar=(progressBar)view.findViewById(R.id.progressBar);
//接收上一活动发送的ListView已单击项值。
TempItem=getActivity().getIntent().getExtras().getString(“ListViewValue1”);
//调用方法筛选学生记录并打开所选记录。
HttpWebCall(临时项目);
//添加单击删除按钮上的侦听器。
返回视图;
}
//删除学生记录的方法
//方法以显示当前记录当前选定的记录
public void HttpWebCall(最终字符串PreviousListViewClickedItem){
类HttpWebCallFunction扩展了AsyncTask{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=ProgressDialog.show(getActivity(),“加载数据”,null,true,true);
}
@凌驾
受保护的void onPostExecute(字符串httpResponseMsg){
super.onPostExecute(httpResponseMsg);
pDialog.disclose();
//将完整的JSon对象存储到字符串变量中。
最终JSONObject=httpResponseMsg;
//将存储的JSOn字符串解析为GetHttpResponse方法。
new EmployeePaymentHistory.GetHttpResponse(getActivity()).execute();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
ResultHash.put(“CustomerID”,参数[0]);
ParseResult=httpParse.postRequest(ResultHash,api.EmployeePayment);
返回解析结果;
}
}
HttpWebCallFunction HttpWebCallFunction=新的HttpWebCallFunction();
httpWebCallFunction.execute(以前的列表视图ClickEdItem);
}
//解析完整的JSON对象。
私有类GetHttpResponse扩展异步任务
{
公共语境;
列表分类列表;
公共GetHttpResponse(上下文)
{
this.context=上下文;
}
@凌驾
受保护的void onPreExecute()
{
super.onPreExecute();
}
@凌驾
受保护的Void doInBackground(Void…arg0)
{
尝试
{
if(FinalJSonObject!=null)
{
JSONArray JSONArray=null;
试一试{
jsonArray=新jsonArray(最终JSONObject);
JSONObject JSONObject;
客户类别;
CategoryList=新的ArrayList();
对于(int i=0;i您需要给出与在android端解析相同的响应输出。如果您试图找到一个键名为的Json对象,但该对象没有响应,那么它将崩溃
在这种情况下,您需要手动处理您认为将为null或空的值,然后使用正常的if语句进行处理
假设jsonObject.getString(“日期”)
对象不是fount,那么使用您可以处理的if语句
if(jsonObject.getString("date") != null){
//code here
}
如果JSON为空或null,它将不会崩溃,请在视图中设置JSON值的位置设置条件。并显示logcathow以设置条件。请发送代码,在try catch中写入获取错误的行,或使用验证案例if@MunawarAkram我想问题出在适配器上。请显示logcat和适配器代码。
if(jsonObject.getString("date") != null){
//code here
}