我有这个错误org.json.JSONObject无法转换为JSONArray(I have this error org.json.JSONObject cannot be converted to

编程入门 行业动态 更新时间:2024-10-28 11:23:46
我有这个错误org.json.JSONObject无法转换为JSONArray(I have this error org.json.JSONObject cannot be converted to JSONArray)

这是我的代码,我正在使用json和齐射我想在下一个活动中显示响应,请详细告诉我您的答案,因为我是android中的新成员

public class MainActivity extends AppCompatActivity { Button btn_logn; EditText et_1, et_2; String url; String username, password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_1 = (EditText) findViewById(R.id.email_one); et_2 = (EditText) findViewById(R.id.pass_two); btn_logn = (Button) findViewById(R.id.btn_login); btn_logn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { login(); } }); } public void login() { username = et_1.getText().toString(); password = et_2.getText().toString(); url = "http://rahatmedia.com/ivy_distribution_test/getEmployeeData.php?" + "email=" + username + "&password=" + password; if (et_1.equals("") || et_2.equals("")) { Toast.makeText(MainActivity.this, "some thing went wrong", Toast.LENGTH_SHORT).show(); } else { final StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String response) { try { JSONArray jsonArray = new JSONArray(response); JSONObject jsonObject = jsonArray.getJSONObject(0); String code = jsonObject.getString("code"); if (code.equals("login_fail")) { Toast.makeText(MainActivity.this, "error in login", Toast.LENGTH_SHORT).show(); } else { Intent i = new Intent(MainActivity.this, display.class); Bundle bundle = new Bundle(); bundle.putString("name", jsonObject.getString("name")); bundle.putString("email", jsonObject.getString("email")); bundle.putString("license_id ", jsonObject.getString("license_id")); bundle.putString("current_loc ", jsonObject.getString("current_loc")); bundle.putString("status ", jsonObject.getString("status")); bundle.putString("password ", jsonObject.getString("password")); bundle.putString("passcode ", jsonObject.getString("passcode")); bundle.putString("mobile ", jsonObject.getString("mobile")); bundle.putString("idcard ", jsonObject.getString("idcard")); bundle.putString("address ", jsonObject.getString("address")); bundle.putString("emptype ", jsonObject.getString("emptype")); bundle.putString("gst_reg ", jsonObject.getString("gst_reg")); bundle.putString("job_status ", jsonObject.getString("job_status")); bundle.putString("joiningdate ", jsonObject.getString("joiningdate")); bundle.putString("timestamp ", jsonObject.getString("timestamp ")); i.putExtras(bundle); startActivity(i); Log.e("response", response); } } catch (JSONException e) { Toast.makeText(MainActivity.this, "error in jasonexception", Toast.LENGTH_SHORT).show(); Log.e("JSONException response", String.valueOf(e)); e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { Toast.makeText(MainActivity.this, "error in ErrorListener", Toast.LENGTH_SHORT).show(); } }) { @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> params = new HashMap<String, String>(); params.put("username", username); params.put("password", password); return params; } }; singalton.getinstance(MainActivity.this).addtorequestque(stringRequest); } }

这是我的单身班

public class singalton { private static singalton minstance; private RequestQueue requestQueue; private Context context; private singalton(Context cont) { context = cont; requestQueue = getRequestQueue(); public RequestQueue getRequestQueue() { if (requestQueue == null) { requestQueue = Volley.newRequestQueue(context.getApplicationContext()); } return requestQueue; } public static synchronized singalton getinstance(Context context) { if (minstance == null) { minstance = new singalton(context); } return minstance; } public <T> void addtorequestque(Request<T> request) { requestQueue.add(request); }

这是我的展示类

public class display extends AppCompatActivity { TextView tv_one, tv_two, tv_three, tv_four, tv_five, tv_six, tv_seven, tv_eight, tv_nine, tv_ten, tv_eleven, tv_twelve, tv_thirdeen, tv_fourteen, tv_fifteen; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display); tv_one = (TextView) findViewById(R.id.txt_1); tv_two = (TextView) findViewById(R.id.txt_2); tv_three = (TextView) findViewById(R.id.txt_3); tv_four = (TextView) findViewById(R.id.txt_4); tv_five = (TextView) findViewById(R.id.txt_5); tv_six = (TextView) findViewById(R.id.txt_6); tv_seven = (TextView) findViewById(R.id.txt_7); tv_eight = (TextView) findViewById(R.id.txt_8); tv_nine = (TextView) findViewById(R.id.txt_9); tv_ten = (TextView) findViewById(R.id.txt_10); tv_eleven = (TextView) findViewById(R.id.txt_11); tv_twelve = (TextView) findViewById(R.id.txt_12); tv_thirdeen = (TextView) findViewById(R.id.txt_13); tv_fourteen = (TextView) findViewById(R.id.txt_14); tv_fifteen = (TextView) findViewById(R.id.txt_15); Bundle bundle = getIntent().getExtras(); tv_one.setText("name" + bundle.getString("name")); tv_two.setText("email" + bundle.getString("email")); tv_three.setText("license_id " + bundle.getString("license_id")); tv_four.setText("current_loc " + bundle.getString("current_loc")); tv_five.setText("status " + bundle.getString("status")); tv_six.setText("password " + bundle.getString("password")); tv_seven.setText("passcode " + bundle.getString("passcode")); tv_eight.setText("mobile " + bundle.getString("mobile")); tv_nine.setText("idcard " + bundle.getString("idcard")); tv_ten.setText("address " + bundle.getString("address")); tv_eleven.setText("emptype " + bundle.getString("emptype")); tv_twelve.setText("gst_reg " + bundle.getString("gst_reg")); tv_thirdeen.setText("job_status " + bundle.getString("job_status")); tv_fourteen.setText("joiningdate " + bundle.getString("joiningdate")); tv_fifteen.setText("timestamp " + bundle.getString("timestamp ")); }

This is my code and I am using json and volley I want to show response in next activity please tell me your answer in detail because I am new in android

public class MainActivity extends AppCompatActivity { Button btn_logn; EditText et_1, et_2; String url; String username, password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_1 = (EditText) findViewById(R.id.email_one); et_2 = (EditText) findViewById(R.id.pass_two); btn_logn = (Button) findViewById(R.id.btn_login); btn_logn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { login(); } }); } public void login() { username = et_1.getText().toString(); password = et_2.getText().toString(); url = "http://rahatmedia.com/ivy_distribution_test/getEmployeeData.php?" + "email=" + username + "&password=" + password; if (et_1.equals("") || et_2.equals("")) { Toast.makeText(MainActivity.this, "some thing went wrong", Toast.LENGTH_SHORT).show(); } else { final StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String response) { try { JSONArray jsonArray = new JSONArray(response); JSONObject jsonObject = jsonArray.getJSONObject(0); String code = jsonObject.getString("code"); if (code.equals("login_fail")) { Toast.makeText(MainActivity.this, "error in login", Toast.LENGTH_SHORT).show(); } else { Intent i = new Intent(MainActivity.this, display.class); Bundle bundle = new Bundle(); bundle.putString("name", jsonObject.getString("name")); bundle.putString("email", jsonObject.getString("email")); bundle.putString("license_id ", jsonObject.getString("license_id")); bundle.putString("current_loc ", jsonObject.getString("current_loc")); bundle.putString("status ", jsonObject.getString("status")); bundle.putString("password ", jsonObject.getString("password")); bundle.putString("passcode ", jsonObject.getString("passcode")); bundle.putString("mobile ", jsonObject.getString("mobile")); bundle.putString("idcard ", jsonObject.getString("idcard")); bundle.putString("address ", jsonObject.getString("address")); bundle.putString("emptype ", jsonObject.getString("emptype")); bundle.putString("gst_reg ", jsonObject.getString("gst_reg")); bundle.putString("job_status ", jsonObject.getString("job_status")); bundle.putString("joiningdate ", jsonObject.getString("joiningdate")); bundle.putString("timestamp ", jsonObject.getString("timestamp ")); i.putExtras(bundle); startActivity(i); Log.e("response", response); } } catch (JSONException e) { Toast.makeText(MainActivity.this, "error in jasonexception", Toast.LENGTH_SHORT).show(); Log.e("JSONException response", String.valueOf(e)); e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { Toast.makeText(MainActivity.this, "error in ErrorListener", Toast.LENGTH_SHORT).show(); } }) { @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> params = new HashMap<String, String>(); params.put("username", username); params.put("password", password); return params; } }; singalton.getinstance(MainActivity.this).addtorequestque(stringRequest); } }

This is my singleton class

public class singalton { private static singalton minstance; private RequestQueue requestQueue; private Context context; private singalton(Context cont) { context = cont; requestQueue = getRequestQueue(); public RequestQueue getRequestQueue() { if (requestQueue == null) { requestQueue = Volley.newRequestQueue(context.getApplicationContext()); } return requestQueue; } public static synchronized singalton getinstance(Context context) { if (minstance == null) { minstance = new singalton(context); } return minstance; } public <T> void addtorequestque(Request<T> request) { requestQueue.add(request); }

this is my display class

public class display extends AppCompatActivity { TextView tv_one, tv_two, tv_three, tv_four, tv_five, tv_six, tv_seven, tv_eight, tv_nine, tv_ten, tv_eleven, tv_twelve, tv_thirdeen, tv_fourteen, tv_fifteen; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display); tv_one = (TextView) findViewById(R.id.txt_1); tv_two = (TextView) findViewById(R.id.txt_2); tv_three = (TextView) findViewById(R.id.txt_3); tv_four = (TextView) findViewById(R.id.txt_4); tv_five = (TextView) findViewById(R.id.txt_5); tv_six = (TextView) findViewById(R.id.txt_6); tv_seven = (TextView) findViewById(R.id.txt_7); tv_eight = (TextView) findViewById(R.id.txt_8); tv_nine = (TextView) findViewById(R.id.txt_9); tv_ten = (TextView) findViewById(R.id.txt_10); tv_eleven = (TextView) findViewById(R.id.txt_11); tv_twelve = (TextView) findViewById(R.id.txt_12); tv_thirdeen = (TextView) findViewById(R.id.txt_13); tv_fourteen = (TextView) findViewById(R.id.txt_14); tv_fifteen = (TextView) findViewById(R.id.txt_15); Bundle bundle = getIntent().getExtras(); tv_one.setText("name" + bundle.getString("name")); tv_two.setText("email" + bundle.getString("email")); tv_three.setText("license_id " + bundle.getString("license_id")); tv_four.setText("current_loc " + bundle.getString("current_loc")); tv_five.setText("status " + bundle.getString("status")); tv_six.setText("password " + bundle.getString("password")); tv_seven.setText("passcode " + bundle.getString("passcode")); tv_eight.setText("mobile " + bundle.getString("mobile")); tv_nine.setText("idcard " + bundle.getString("idcard")); tv_ten.setText("address " + bundle.getString("address")); tv_eleven.setText("emptype " + bundle.getString("emptype")); tv_twelve.setText("gst_reg " + bundle.getString("gst_reg")); tv_thirdeen.setText("job_status " + bundle.getString("job_status")); tv_fourteen.setText("joiningdate " + bundle.getString("joiningdate")); tv_fifteen.setText("timestamp " + bundle.getString("timestamp ")); }

最满意答案

从这改变你的三条线

JSONArray jsonArray = new JSONArray(response); JSONObject jsonObject = jsonArray.getJSONObject(0); String code = jsonObject.getString("code");

对此

JSONObject jsonObject = new JSONObject(response); String code = jsonObject.getString("data");

Change your three line from this

JSONArray jsonArray = new JSONArray(response); JSONObject jsonObject = jsonArray.getJSONObject(0); String code = jsonObject.getString("code");

to this

JSONObject jsonObject = new JSONObject(response); String code = jsonObject.getString("data");

更多推荐

本文发布于:2023-07-14 23:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1108315.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我有   转换为   错误   org   JSONObject

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!