这是我的代码,我正在使用json和凌云我想在下一个活动中显示响应,请详细告诉我您的答案,因为我是新的android我有这个错误org.json.JSONObject无法转换为JSONArray
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 "));
}
正试图地图的JSONObject成JSONArray。我认为这部分代码是错误的:'JSONArray jsonArray = new JSONArray(response);' –
好的请告诉我我该怎么做? –
将其更改为'JSONObject',请阅读以下内容:http://www.androidhive.info/2012/01/android-json-parsing-tutorial/,或许nextime改用替代方式(对初学者更友好):http:/ /www.vogella.com/tutorials/Retrofit/article.html –