我有这个错误org.json.JSONObject无法转换为JSONArray

时间:2017-04-10 13:20:45

标签: android json parsing

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

this.props.article

这是我的单身人士课程

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);
}

2 个答案:

答案 0 :(得分:0)

在你的api。 我已经尝试过这样的api http://rahatmedia.com/ivy_distribution_test/getEmployeeData.php?email=rasmi@gmail.com&password=123456

它回馈了jsonObj。因此,在您的登录方法中,您已将其视为JSONArray ..请选择JSONObject js = new JSONObject(responce);

答案 1 :(得分:0)

从这个

改变你的三行
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");