jsonarray值這是我JSON編碼碼如何顯示在安卓
{"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}
如何轉換成JSONObject的。例如"name":["The name field is required."]
我想向用戶顯示名稱的價值任何人都可以告訴我如何做到這一點。
這是我的Android代碼
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();
}
}
}
你有什麼錯誤? – 2014-12-06 06:30:20
我把它放到我的logcat中 {「success」:0,「detail」:{「phone」:[「電話字段是必需的。」],「address」:[「地址字段是必需的。 ],「email」:[「電子郵件字段是必需的。」],「密碼」:[「密碼字段是必需的。」],「名稱」:[「名稱字段是必需的。」]}} 但不知道如何在textview中顯示 – jayant 2014-12-06 06:45:03
您是否嘗試過我的答案? – 2014-12-06 06:47:47