获取Android应用程序中的致命错误
我制作了一个应用程序,它以姓名、联系人号码和其他字段作为输入,并将这些数据保存在mysql数据库的服务器上。当android应用程序中没有对输入字段(EditText)进行验证时,这可以正常工作,但在进行验证时会出现致命错误。下面是我的带有验证和logcat的代码。同样,如果我从活动中删除了验证代码,那么数据成功地被SAE发送到服务器,并且我得到了Json响应。但通过验证,它显示出错误,我不知道为什么会发生这种情况?请帮帮我获取Android应用程序中的致命错误,android,json,android-asynctask,Android,Json,Android Asynctask,我制作了一个应用程序,它以姓名、联系人号码和其他字段作为输入,并将这些数据保存在mysql数据库的服务器上。当android应用程序中没有对输入字段(EditText)进行验证时,这可以正常工作,但在进行验证时会出现致命错误。下面是我的带有验证和logcat的代码。同样,如果我从活动中删除了验证代码,那么数据成功地被SAE发送到服务器,并且我得到了Json响应。但通过验证,它显示出错误,我不知道为什么会发生这种情况?请帮帮我 ..... public static SharedPreferenc
.....
public static SharedPreferences sellcro;
public static String name11;
public static String mobile11;
public static String weight11;
public static String name111 = "";
public static String mobile111 = "";
public static String weight111 = "";
JSONParser jsonParser = new JSONParser();
EditText inputName;
EditText inputMobile;
EditText inputWeight;
private static final String TAG_SUCCESS = "success";
.....
inputName = (EditText) findViewById(R.id.editText1);
inputMobile = (EditText) findViewById(R.id.editText2);
inputWeight = (EditText) findViewById(R.id.editText3);
.....
btnCreateProduct.setOnClickListener....
name11 = inputName.getText().toString();
if (!isValidName(name11)) {
inputName.setError("Please Enter Proper Name");
}
else{
name111 = "right";
}
mobile11 = inputMobile.getText().toString();
if (!isValidMobile(mobile11)) {
inputMobile.setError("Please Enter 10 Digit Mobile Number");
}
else{
mobile111 = "right";
}
weight11 = inputWeight.getText().toString();
if (!isValidWeight(weight11)) {
inputWeight.setError("Weight must be maximum of 9 character");
}
else{
weight111 = "right";
}
if(mobile111.equals("right") && weight111.equals("right") && name111.equals("right")){
new CreateNewProduct().execute();
}else{}
}
});}
private boolean isValidWeight(String weight11) {
// TODO Auto-generated method stub
if(weight11 != null && weight11.length()>0 && weight11.length()<10){
return true;
}
return false;
}
private boolean isValidMobile(String mobile11) {
// TODO Auto-generated method stub
if(mobile11 != null && mobile11.length()>0 && mobile11.length()==10){
return true;
}
return false;
}
private boolean isValidName(String name11) {
// TODO Auto-generated method stub
if(name11 != null && name11.length()<60){
String name_pattern = "[a-zA-z]+([ '-][a-zA-Z]+)*";
Pattern pattern = Pattern.compile(name_pattern);
Matcher matcher = pattern.matcher(name11);
return matcher.matches();
}
return false;
}
class CreateNewProduct extends AsyncTask<...............
.......
protected String doInBackground(String... args) {
..........
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
// closing this screen
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
...........
。。。。。
公开静态共享参考sellcro;
公共静态字符串名称11;
公共静态字符串mobile1;
公共静态字符串权重11;
公共静态字符串name111=“”;
公共静态字符串mobile111=“”;
公共静态字符串weight111=“”;
JSONParser JSONParser=新的JSONParser();
编辑文本输入名;
编辑文本输入;
编辑文本输入权重;
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
.....
inputName=(EditText)findViewById(R.id.editText1);
inputMobile=(EditText)findViewById(R.id.editText2);
inputWeight=(EditText)findViewById(R.id.editText3);
.....
btnCreateProduct.setOnClickListener。。。。
name11=inputName.getText().toString();
如果(!isValidName(名称11)){
inputName.setError(“请输入正确的名称”);
}
否则{
name111=“右”;
}
mobile11=inputMobile.getText().toString();
如果(!isValidMobile(mobile11)){
inputMobile.setError(“请输入10位手机号码”);
}
否则{
mobile111=“右”;
}
weight11=inputWeight.getText().toString();
如果(!isValidWeight(权重11)){
inputWeight.setError(“重量必须最多为9个字符”);
}
否则{
权重111=“右”;
}
if(mobile111.equals(“right”)和weights 111.equals(“right”)和name 111.equals(“right”)){
新建CreateNewProduct().execute();
}else{}
}
});}
私有布尔值isValidWeight(字符串权重11){
//TODO自动生成的方法存根
如果(weight11!=null&&weight11.length()>0&&weight11.length()0&&mobile11.length()==10){
返回true;
}
返回false;
}
私有布尔值isValidName(字符串名称11){
//TODO自动生成的方法存根
如果(name11!=null&&name11.length()@EDIT
更改编码
//BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
BufferedReader reader = new BufferedReader(new InputStreamReader(is, HTTP.UTF_8), 8);
此外,可能存在一些不需要的字符,请使用
return new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1));
if(name.getText().toString().equals(“”){
Toast.makeText(BookAppointment.this,
“输入名称”,吐司。长度(短)
.show();
}else if(mobile.getText().toString().equals(“”){
Toast.makeText(BookAppointment.this,
“输入移动”,吐司。长度(短)
.show();
}
否则{
如果(!txt_name.isEmpty()||!txt_mob.isEmpty())
如果(!isValidEmail(email.getText().toString())){
Toast.makeText(getApplicationContext(),
“无效电子邮件”,Toast.LENGTH_LONG.show();
}
否则{
字符串值=null;
试一试{
values=“name=“+URLEncoder.encode(txt_名称,“UTF-8”)
+“&mobile=“+URLEncoder.encode(txt_mob,“UTF-8”);
}捕获(例外e){
//TODO:处理异常
e、 printStackTrace();
}
new BookAppointmentTask().execute(值);
}
}
}
}
});
}
私有类BookAppointmentTask扩展
异步任务{
进展性帕金森病;
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
pd=新建进度对话框(BookAppointment.this);
pd.SetUndeterminate(真);
设置消息(“请稍候…”);
pd.可设置可取消(假);
pd.SetUndeterminate(真);
pd.show();
super.onPreExecute();
}
@凌驾
受保护的双DOIN背景(字符串…arg0){
//TODO自动生成的方法存根
postData(arg0[0]);
返回null;
}
公共void postData(字符串值){
//TODO自动生成的方法存根
//创建一个新的HttpClient和Post头
HttpClient HttpClient=新的DefaultHttpClient();
字符串BOOK\u APPOINTMENT=约会\u URL+值;
System.out.println(“URL\u BOOK\u APPOINTMENT=“+BOOK\u APPOINTMENT”);
HttpPost HttpPost=新的HttpPost(预约);
试一试{
//执行
//BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
BufferedReader reader = new BufferedReader(new InputStreamReader(is, HTTP.UTF_8), 8);
return new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1));
if(name.getText().toString().equals("")) {
Toast.makeText(BookAppointment.this,
"Enter Name", Toast.LENGTH_SHORT)
.show();
}else if(mobile.getText().toString().equals("")) {
Toast.makeText(BookAppointment.this,
"Enter mobile", Toast.LENGTH_SHORT)
.show();
}
else {
if (!txt_name.isEmpty() || !txt_mob.isEmpty()
if (!isValidEmail(email.getText().toString())) {
Toast.makeText(getApplicationContext(),
"Invalid Email", Toast.LENGTH_LONG).show();
}
else {
String values = null;
try {
values = "name=" + URLEncoder.encode(txt_name, "UTF-8")
+ "&mobile=" + URLEncoder.encode(txt_mob, "UTF-8");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
new BookAppointmentTask().execute(values);
}
}
}
}
});
}
private class BookAppointmentTask extends
AsyncTask<String, Integer, Double> {
ProgressDialog pd;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
pd = new ProgressDialog(BookAppointment.this);
pd.setIndeterminate(true);
pd.setMessage("Please wait...");
pd.setCancelable(false);
pd.setIndeterminate(true);
pd.show();
super.onPreExecute();
}
@Override
protected Double doInBackground(String... arg0) {
// TODO Auto-generated method stub
postData(arg0[0]);
return null;
}
public void postData(String values) {
// TODO Auto-generated method stub
// Create a new HttpClient and Post Header
HttpClient httpClient = new DefaultHttpClient();
String BOOK_APPOINTMENT = APPOINTMENT_URL + values;
System.out.println("URL_BOOK_APPOINTMENT = " + BOOK_APPOINTMENT);
HttpPost httpPost = new HttpPost(BOOK_APPOINTMENT);
try {
// Execute HTTP Post Request
HttpResponse httpResponse = httpClient.execute(httpPost);
String json = EntityUtils.toString(httpResponse.getEntity());
JSONObject jsonObject = new JSONObject(json);
message = jsonObject.getString("message");
System.out.println("MESSAGE_STATUS = " + message);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
@Override
protected void onPostExecute(Double result) {
// TODO Auto-generated method stub
pd.dismiss();
super.onPostExecute(result);
Toast.makeText(BookAppointment.this, message, Toast.LENGTH_SHORT).show();
name.setText("");
mobile.setText("");
}
}
Try this and let me know if it works