我有php脚本将json返回给android。成功消息没问题。但是我将如何处理表单验证错误消息,如“用户名已存在”,“无效的学生ID”等,通过android在json_encode内部。我尝试将数据类型更改为JSONObject,但它给了我很多红线错误。
任何建议或某事谢谢你们。
public class httpRegister extends AsyncTask <String, Integer, String> {
int success;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
String studId = studentId.getText().toString();
String uName = username.getText().toString();
String pWord = password.getText().toString();
String cPWord = cfmPassword.getText().toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("studNum", studId));
params.add(new BasicNameValuePair("username", uName));
params.add(new BasicNameValuePair("password", pWord));
params.add(new BasicNameValuePair("cfmPassword", cPWord)
JSONParser parser = new JSONParser(); my Class in other activity
JSONObject json = parser.requestHttp(REG_URL, "POST", params);
success = json.getInt(TAG_SUC);//get success msg
if(success == 1){ //No problem here
return json.getString(TAG_MSG);
}else{
//If there's invalid input php script return here
return json; //Problem returning json to PostExecute
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
//User JSONObject here to activity
}
}
答案 0 :(得分:0)
尝试类似这样的事情
public class httpRegister extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
String studId = studentId.getText().toString();
String uName = username.getText().toString();
String pWord = password.getText().toString();
String cPWord = cfmPassword.getText().toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("studNum", studId));
params.add(new BasicNameValuePair("username", uName));
params.add(new BasicNameValuePair("password", pWord));
params.add(new BasicNameValuePair("cfmPassword", cPWord)
JSONParser parser = new JSONParser(); my Class in other activity
JSONObject json = parser.requestHttp(REG_URL, "POST", params);
return json.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "Error";
}
@Override
protected void onPostExecute(String result) {
if(result.equals("Error")){
//SHOW ERROR MSG
}else{
//doSomething
}
}
}
答案 1 :(得分:0)
您可以这样做:
public class httpRegister extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
String studId = studentId.getText().toString();
String uName = username.getText().toString();
String pWord = password.getText().toString();
String cPWord = cfmPassword.getText().toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("studNum", studId));
params.add(new BasicNameValuePair("username", uName));
params.add(new BasicNameValuePair("password", pWord));
params.add(new BasicNameValuePair("cfmPassword", cPWord));
JSONParser parser = new JSONParser();
// my Class in other activity
JSONObject json = parser.requestHttp(REG_URL, "POST", params);
return "Success";
} catch (Exception e) {
e.printStackTrace();
}
return "Error";
}
@Override
protected void onPostExecute(String result) {
if (result.equals("Success")) {
// DO SOMETHING
} else if (result.equals("Error")) {
// SHOW ERROR MSG
}
if (result.equals("Username_Exists")) {
// show toast
}
}
}
答案 2 :(得分:0)
要返回JSONObject
,请尝试以下操作:
public class httpRegister extends AsyncTask <String, Void, JSONObject> {
int success;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
}
@Override
protected JSONObject doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
String studId = studentId.getText().toString();
String uName = username.getText().toString();
String pWord = password.getText().toString();
String cPWord = cfmPassword.getText().toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("studNum", studId));
params.add(new BasicNameValuePair("username", uName));
params.add(new BasicNameValuePair("password", pWord));
params.add(new BasicNameValuePair("cfmPassword", cPWord)
JSONParser parser = new JSONParser(); my Class in other activity
JSONObject json = parser.requestHttp(REG_URL, "POST", params);
return json;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
if(result!=null){
// Get Data from JSONResponce here
}
success = json.getInt(TAG_SUC);//get success msg
//User JSONObject here to activity
}
}
答案 3 :(得分:0)
问题解决了,非常感谢你对我的问题的快速建议我在这里提出问题。我虽然stackoverflow是不活跃的网站因此。当我看到帖子的日期时,评论似乎是3到5年前我在这里搜索。哈哈无论如何。
首先,我喜欢这样做
AsyncTask <String, Integer, String>{
}
protected String doInBackground(String... arg0){
}
protected void onPostExecute(String result){
}
然后将其更改为
AsyncTask<JSONObject, Object, JSONObject> //But I don't know why here haha
protected JSONObject doInBackground(JSONObject... arg0)// And the dot dot dot{
//here is
return json;
}
protected void onPostExecute(JSONObject result){
//here I use json
int success2;
try {
success2 = result.getInt(TAG_SUC);
if(success2 == 1){
//get success msg
}else{
//get msg's of form validation
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
有任何改进的想法吗? ^ ___ ^ thx to all。