【发布时间】:2015-09-29 21:24:58
【问题描述】:
我在 android 中创建了一个表单,其中包括名字、姓氏、手机号码等字段。 我正在使用网络服务将此数据发送到服务器并且它完美地发生。现在我想检查互联网连接,如果用户在填写表格并且用户提交表格时没有连接到互联网,表格应该上传到服务器每当用户回到互联网连接时。 谁能告诉我实现此功能的方法。 我尝试使用广播接收器检查互联网连接,但无法上传表单。我还必须将表单字段保存在永久存储中。目前我将它们保存在 bean 类中。
我正在粘贴我的完整表单活动类代码。 如果需要任何更改,请告诉我
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Boolean isValidEmail = false;
String first_name = firstname.getText().toString().trim();
String last_name = lastname.getText().toString().trim();
String country = nation.getText().toString().trim();
String mail = email.getText().toString().trim();
String firm = company.getText().toString().trim();
String mobile = phone.getText().toString().trim();
String post = title.getText().toString().trim();
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
if (first_name.equalsIgnoreCase("")) {
Toast.makeText(getApplicationContext(), "Please enter your first name.", Toast.LENGTH_LONG).show();
} else {
registrationBean.setFname(first_name);
if (last_name.equalsIgnoreCase("")) {
Toast.makeText(getApplicationContext(), "Please enter your last name.", Toast.LENGTH_LONG).show();
} else {
registrationBean.setLname(last_name);
if (firm.equalsIgnoreCase("")) {
Toast.makeText(getApplicationContext(), "Please enter your company name.", Toast.LENGTH_LONG).show();
} else {
registrationBean.setCompany(firm);
if (post.equalsIgnoreCase("")) {
Toast.makeText(getApplicationContext(), "Please enter your designation", Toast.LENGTH_LONG).show();
} else {
registrationBean.setTitle(post);
if (mobile.equalsIgnoreCase("")) {
Toast.makeText(getApplicationContext(), "Please enter your phone number", Toast.LENGTH_SHORT).show();
} else {
registrationBean.setPhone(mobile);
if (mail.equalsIgnoreCase("") && !mail.matches(emailPattern)) {
Toast.makeText(getApplicationContext(), "Invalid Email", Toast.LENGTH_LONG).show();
} else {
registrationBean.setMail(mail);
if (country.equalsIgnoreCase("")) {
Toast.makeText(getApplicationContext(), "Please enter your country of residence", Toast.LENGTH_SHORT).show();
} else {
registrationBean.setNation(country);
if (checked==true && registrationBean!=null)
{
}
else {
Toast.makeText(getApplicationContext(),"Please accept terms and conditions",Toast.LENGTH_LONG).show();
}
}
}
}
}
}
}
}
}
});
if (img_path.equalsIgnoreCase("")) {
retake_photo.setAlpha((float) 0.6);
}
}
private void selectImage() {
final CharSequence[] options = {"Take a Picture", "Choose from Gallery", "Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(ActivityForm.this);
builder.setCancelable(false);
builder.setTitle("Select Profile Photo!");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take a Picture")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(Environment.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
img_path = Environment.getExternalStorageDirectory() + "/temp.jpg";
startActivityForResult(intent, 1);
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
if (options[item].equals("Choose from Gallery")) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Intent photoPickerIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/*");
photoPickerIntent.addCategory(photoPickerIntent.CATEGORY_OPENABLE);
startActivityForResult(photoPickerIntent, 2);
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
} else {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
img_path = MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString();
startActivityForResult(intent, 2);
}
}
}
});
builder.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case 1:
try {
bitmap = AppUtils.decodeFile(file.getPath(), 100, 100);
registrationBean.setImage(file.getPath());
// ExifInterface ei = new ExifInterface(file.getPath());
} catch (Exception e) {
e.printStackTrace();
}
if (bitmap != null) {
iv_profile.setImageBitmap(bitmap);
retake_photo.setAlpha(1);
}
break;
case 2:
try {
String uriImage;
imageUri = imageReturnedIntent.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
imageStream.close();
if (selectedImage != null) {
iv_profile.setImageBitmap(selectedImage);
retake_photo.setAlpha(1);
}
uriImage=imageUri.toString();
registrationBean.setImage(uriImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
private void callRegistrationService()
{
String fname=registrationBean.getFname();
String lname=registrationBean.getLname();
String company=registrationBean.getCompany();
String mobile=registrationBean.getPhone();
String designation=registrationBean.getTitle();
String image=getPath(imageUri);
String email=registrationBean.getMail();
String country=registrationBean.getNation();
//email,firstName,lastName,userImage,company,title,phone,country
new RegistrationAsyncTask(this, email, fname, lname, image, company, designation, mobile, country).execute();
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
final ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isAvailable() || mobile.isAvailable()) {
// Do something
callRegistrationService();
}
}
}
【问题讨论】:
-
如此讨厌的
if...else..if...else构造。您需要实现某种验证器,它将以适当的方式实现这一点。或者,至少,您将其更改为if (!check) { Toast.makeText(); return; }序列。 -
这是表单字段的验证器