Android : How to show jsonarray value in android

on Friday, December 5, 2014


This is my json encoded code



{"detail":
{"name":["The name field is required."],
"password":["The password field is required."],
"email":["The email field is required."],
"phone":["The phone field is required."],
"address":["The address field is required."]},"success":0}


How do i convert it into jsonobject. e.g "name":["The name field is required."] i want to show the value of name to user can anyone please tell me how to do this.


This is my android code



public class AccountRegister extends Activity {
private ProgressDialog pDialog;
String password;
Button bnt_Submit;
EditText edt_email,edt_password,edt_name,edt_phone,edt_address;

JSONParser jsonParser = new JSONParser();

private static String url_create_product = "http://192.168.1.2/laravel/public/registerUser";


private static final String TAG_SUCCESS = "success";
@Override


protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);


bnt_Submit =(Button)findViewById(R.id.btnsubmit);
edt_email = (EditText)findViewById(R.id.Edt_Email);
edt_password = (EditText)findViewById(R.id.Edt_Password);
edt_name = (EditText)findViewById(R.id.Edt_Name);
edt_phone = (EditText)findViewById(R.id.Edt_Phone);
edt_address = (EditText)findViewById(R.id.Edt_Address);



bnt_Submit.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub

password = edt_password.getText().toString();
if (!isValidPassword(password)) {
edt_password.setError("The Passward must be at least 8 character ");
}
new RegisterUser().execute();



}
});



}


private boolean isValidPassword(String pass) {
if (pass != null && pass.length() > 7) {
return true;
}
return false;
}

class RegisterUser extends AsyncTask<String, String, String> {



@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();

pDialog = new ProgressDialog(AccountRegister.this);
pDialog.setMessage("Registering...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();


}

@Override
protected String doInBackground(String... params) {

// TODO Auto-generated method stub

String name =edt_name.getText().toString();

String email =edt_email.getText().toString();
String phone =edt_phone.getText().toString();
String address =edt_address.getText().toString();




List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params1.add(new BasicNameValuePair("name", name));
params1.add(new BasicNameValuePair("password", password));
params1.add(new BasicNameValuePair("email", email));
params1.add(new BasicNameValuePair("phone", phone));
params1.add(new BasicNameValuePair("address", address));



JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params1);


Log.d("Create Response", json.toString());


try {
int success = json.getInt(TAG_SUCCESS);

if (success == 1) {


} else if(success == 0) {



JSONArray arr = new JSONArray("detail");

for(int i = 0; i < arr.length(); i++){

JSONObject c = arr.getJSONObject(i);
JSONArray ar_in = c.getJSONArray("name");

for(int j = 0; j < ar_in.length(); j++){
Log.v("result--", ar_in.getString(j));
}
}





}
} catch (JSONException e) {
e.printStackTrace();
}

return null;
}


protected void onPostExecute(String file_url) { // dismiss the dialog once done



pDialog.dismiss();
}


}
}

0 comments:

Post a Comment