我制作了一个应用程序,使用齐射从api获取json数组,成功登录时将显示json数组。
这是我的json响应。
"status": "success",
"tampil": [
{
"serial": "f9fa46a266d5b086053f2a8d3c850805",
"sales_order_no_car": "SONH-19/0009666",
"sales_order_no_materials": null,
"sales_order_no_building": null,
"order_date": "2019-02-13 00:00:00",
"urgent_status": null,
"note2": null,
"installation_address": "HONDA CAKRA RADEN INTEN",
"car_model": "HRV 2017",
"full_name": null,
"name_showroom": "PT. PANGUKIRGADING CAKRAUTAMA",
"window_film_desc": "V-KOOL VRX60,V-KOOL BX15,V-KOOL BX05,V-KOOL BX05",
"chassis_no": "MHRRU1850JJ804432",
"installation_type": "01",
"sales_type": "1",
"user_serial": "4cd6110cba8b30c4960ca1108f1ffba4"
},
{
"serial": "ccde84a114bed8401f97a8056b2b5db2",
"sales_order_no_car": "SONH-19/0009667",
"sales_order_no_materials": null,
"sales_order_no_building": null,
"order_date": "2019-02-13 00:00:00",
"urgent_status": null,
"note2": null,
"installation_address": "HONDA CAKRA RADEN INTEN",
"car_model": "HRV 2017",
"full_name": null,
"name_showroom": "PT. PANGUKIRGADING CAKRAUTAMA",
"window_film_desc": "V-KOOL VRX60,V-KOOL BX15,V-KOOL BX15",
"chassis_no": "MHRRU1860KJ800100",
"installation_type": "01",
"sales_type": "1",
"user_serial": "4cd6110cba8b30c4960ca1108f1ffba4"
},
{
"serial": "ca07913d6e4f2b28fc4dff47f4673727",
"sales_order_no_car": "SONH-19/0009668",
"sales_order_no_materials": null,
"sales_order_no_building": null,
"order_date": "2019-02-13 00:00:00",
"urgent_status": null,
"note2": null,
"installation_address": "HONDA CAKRA RADEN INTEN",
"car_model": "NEW CRV 2017",
"full_name": null,
"name_showroom": "PT. PANGUKIRGADING CAKRAUTAMA",
"window_film_desc": "V-KOOL VRX60,V-KOOL BX05,V-KOOL BX05",
"chassis_no": "MHRRW1880JJ811140",
"installation_type": "01",
"sales_type": "1",
"user_serial": "4cd6110cba8b30c4960ca1108f1ffba4"
},
{
"serial": "0d17e67eb5deb6d69ab6ea08004f7fd1",
"sales_order_no_car": "SONH-19/0009669",
"sales_order_no_materials": null,
"sales_order_no_building": null,
"order_date": "2019-02-13 00:00:00",
"urgent_status": null,
"note2": null,
"installation_address": "HONDA CAKRA RADEN INTEN",
"car_model": "NEW CRV 2017",
"full_name": null,
"name_showroom": "PT. PANGUKIRGADING CAKRAUTAMA",
"window_film_desc": "V-KOOL VRX15,V-KOOL BX15,V-KOOL BX05,V-KOOL BX15",
"chassis_no": "MHRRW1880JJ808477",
"installation_type": "01",
"sales_type": "1",
"user_serial": "4cd6110cba8b30c4960ca1108f1ffba4"
},
{
"serial": "85917eb065b0143e93b92e24b9d5175a",
"sales_order_no_car": "SOPKK-19/0001246",
"sales_order_no_materials": null,
"sales_order_no_building": null,
"order_date": "2019-02-12 00:00:00",
"urgent_status": "2",
"note2": null,
"installation_address": "CENTRAL 88 BP VETER - 081282600328",
"car_model": "NEW CRV 2017",
"full_name": null,
"name_showroom": "PT. MAJU MOBILINDO",
"window_film_desc": "V-KOOL VRX-60, V-KOOL BX15",
"chassis_no": "MHRRW3830HJ800733",
"installation_type": "05",
"sales_type": "7",
"user_serial": "4cd6110cba8b30c4960ca1108f1ffba4"
},
{
"serial": "4e7b7ae6aaad80388c6628bc2c72a7fa",
"sales_order_no_car": "SOPKK-18/0001122",
"sales_order_no_materials": null,
"sales_order_no_building": null,
"order_date": "2018-11-16 00:00:00",
"urgent_status": "2",
"note2": null,
"installation_address": "GUDANG SUNTER",
"car_model": "NEW ACCORD 2017",
"full_name": null,
"name_showroom": "PT. IMORA MOTOR",
"window_film_desc": "V-KOOL BX15",
"chassis_no": "MRHCR2640JP810152",
"installation_type": "31",
"sales_type": "7",
"user_serial": "4cd6110cba8b30c4960ca1108f1ffba4"
},
{
"serial": "96b1bceb2bcd7bc78ebda91f833b2671",
"sales_order_no_car": "SOLKL-18/0000019",
"sales_order_no_materials": null,
"sales_order_no_building": null,
"order_date": "2018-03-20 00:00:00",
"urgent_status": "2",
"note2": null,
"installation_address": "JL. JATI 1 NO.8 RT.002/006 SUNGAI BAMBU, T. PRIOK ( PASAR PELITA JAYA/ PASAR POOL YANG BARU) (KLP GADING)",
"car_model": "ALL NEW AVANZA",
"full_name": "MASIDEH",
"name_showroom": "ASTRA INTERNASIONAL TOYOTA SALES OPERATION-SUNTER",
"window_film_desc": "V-KOOL 70",
"chassis_no": "MHKM1CA4JEK083149",
"installation_type": "08",
"sales_type": "7",
"user_serial": "4cd6110cba8b30c4960ca1108f1ffba4"
},
{
"serial": "e494c4b7ec7dcedb4c0d54b3a6c9c85c",
"sales_order_no_car": "SOCH-18/0137485",
"sales_order_no_materials": null,
"sales_order_no_building": null,
"order_date": "2018-02-22 00:00:00",
"urgent_status": "2",
"note2": null,
"installation_address": "CILINCING DOSH-18/0000166",
"car_model": "HRV 2017",
"full_name": null,
"name_showroom": "HONDA MITRA JAYAPURA",
"window_film_desc": "V-KOOL VRX60, V-KOOL BX15",
"chassis_no": "MHRRU1730JJ700410",
"installation_type": "01",
"sales_type": "1",
"user_serial": "4cd6110cba8b30c4960ca1108f1ffba4"
}
]
}
这是我的 loginfragment
代码public class LoginFragment extends Fragment {
public static final String TAG = LoginFragment.class.getSimpleName();
private EditText etEmail, etPass;
private Button btnLogin;
Context context;
ProgressDialog progressDialog;
SharedPrefManager sharedPrefManager;
private VolleyController mInstance;
public LoginFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_login, container, false);
context = getActivity();
etEmail = v.findViewById(R.id.et_email);
etPass = v.findViewById(R.id.et_pass);
btnLogin = v.findViewById(R.id.btn_login);
progressDialog = new ProgressDialog(getActivity());
sharedPrefManager = new SharedPrefManager(getActivity());
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LoginProcess();
}
});
return v;
}
private void LoginProcess() {
final String email = etEmail.getText().toString().trim();
final String pass = etPass.getText().toString().trim();
progressDialog.setMessage("Login process...");
showDialog();
StringRequest stringRequest = new StringRequest(Request.Method.POST, ModelVar.url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject jobj = null;
try{
jobj = new JSONObject(response.toString());
}catch (JSONException e){
e.printStackTrace();
}
String status = null;
try{
status = jobj.getString("status");
}catch (JSONException e){
e.printStackTrace();
}
if(status.contains("success")){
Toast.makeText(context, "SUCCESS LOGIN",Toast.LENGTH_LONG).show();
sharedPrefManager.saveSPBoolean(SharedPrefManager.LOGGED_IN ,true);
hideDialog();
gotoMenuFragment();
}else{
hideDialog();
Toast.makeText(context, "Invalid username or password", Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
hideDialog();
Toast.makeText(context, "The Server Unreachable", Toast.LENGTH_LONG).show();
}
}){
protected Map<String , String > getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put(ModelVar.KEY_USER_ID, email);
params.put(ModelVar.KEY_PASSWORD, pass);
return params;
}
};
Volley.newRequestQueue(getActivity()).add(stringRequest);
}
private void gotoMenuFragment() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, new MenuFragment(), "Menu");
ft.commit();
ft.addToBackStack(null);
}
private void showDialog() {
if (!progressDialog.isShowing())
progressDialog.show();
}
private void hideDialog() {
if (progressDialog.isShowing())
progressDialog.dismiss();
}
}
,这是我的menufragment
代码。我不知道如何将jsonarray
升级到menufragment
。
public class MenuFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
public static final String TAG = MenuFragment.class.getSimpleName();
ListView lvMenu;
AdapterData adapterData;
List<Data> listdata;
VolleyController volleyController;
SwipeRefreshLayout swipeRefreshLayout;
onLogoutListener logoutListener;
public interface onLogoutListener{
public void logoutPerformed();
}
public MenuFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Button btnLogout;
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_menu, container, false);
btnLogout = v.findViewById(R.id.btn_logout);
btnLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
logoutListener.logoutPerformed();
}
});
lvMenu = v.findViewById(R.id.lv_menu);
adapterData = new AdapterData(getActivity(), listdata);
lvMenu.setAdapter(adapterData);
swipeRefreshLayout = v.findViewById(R.id.srl_menu);
swipeRefreshLayout.setOnRefreshListener(this);
return v;
}
private void getUserData() {
listdata.clear();
swipeRefreshLayout.setRefreshing(true);
adapterData.notifyDataSetChanged();
final JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(ModelVar.url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSONObject jobj = null;
try {
jobj = new JSONObject(response.toString());
} catch (JSONException e) {
e.printStackTrace();
}
JSONArray obj = null;
try {
obj = jobj.getJSONArray("tampil");
} catch (JSONException e) {
e.printStackTrace();
}
Log.d(TAG, response.toString());
for (int i = 0; i < obj.length(); i++) {
try {
JSONObject jdata = obj.getJSONObject(i);
//here u can get all field like this
Data item = new Data();
String sonc = jdata.getString("sales_order_no_car");
String address = jdata.getString("installation_address");
String nameShowroom = jdata.getString("name_showroom");
String orderDate = jdata.getString("order_date");
item.setSales_order_no_car(sonc);
item.setInstallation_address(address);
item.setName_showroom(nameShowroom);
item.setOrder_date(orderDate);
listdata.add(item);
} catch (JSONException e) {
e.printStackTrace();
}
}
adapterData.notifyDataSetChanged();
swipeRefreshLayout.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
swipeRefreshLayout.setRefreshing(false);
}
});
VolleyController.getInstance(getActivity()).addToRequestQueue(jsonArrayRequest);
}
@Override
public void onRefresh() {
listdata.clear();
adapterData.notifyDataSetChanged();
getUserData();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
Activity activity = (Activity) context;
logoutListener = (onLogoutListener) activity;
}
}
我的项目出错
E / Android运行时:致命异常:主要 流程:com.scsvkool.autosendemail,PID:7047 java.lang.RuntimeException:无法启动活动ComponentInfo {com.scsvkool.autosendemail / com.scsvkool.autosendemail.MainActivity}: java.lang.NullPointerException:尝试调用接口方法 空对象引用上的'int java.util.List.size()' 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 在android.app.ActivityThread.-wrap11(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5417) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
原因:java.lang.NullPointerException:尝试在空对象上调用接口方法'int java.util.List.size()' 参考 在com.scsvkool.autosendemail.DataModel.AdapterData.getCount(AdapterData.java:34) 在android.widget.ListView.setAdapter(ListView.java:491) 在com.scsvkool.autosendemail.FragmentActivity.MenuFragment.onCreateView(MenuFragment.java:74) 在android.support.v4.app.Fragment.performCreateView(Fragment.java:2439) 在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460) 在android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784) 在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852) 在android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802) 在android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625) 在android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411) 在android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366) 在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273) 在android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273) 在android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229) 在android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201) 在android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:620) 在android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178) 在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237) 在android.app.Activity.performStart(Activity.java:6253) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 在android.app.ActivityThread.-wrap11(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5417) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
您要在适配器上提供空列表
adapterData = new AdapterData(getActivity(), listdata);
在发送给适配器之前先用new初始化“ listdata”,例如
listdata = new ArrayList<Data>;