我现在不考虑SQL注入,一旦我完成这个问题就会这样做
我遇到的问题是当我试图通过AsyncTask在服务器上插入数据时,值被插入两次。我尝试使用计数器来检查AsyncTask是否被调用两次,但值是“1”。
//这是我的班级文件
public class Add extends Activity implements OnItemSelectedListener {
String picturePath;
final String fontPath1 = "fonts/COPRGTB.TTF";
int cnt=0;
// --------------------------------------------
private static int RESULT_LOAD_IMAGE = 1;
private static final int PICK_FROM_GALLERY = 2;
int CAMERA_PIC_REQUEST = 1337;
String SD_CARD_IMAGE_PATH = null;
Bitmap thumbnail = null;
private static final int OG = 4;
Cursor cursor;
private static final int CAMERA_IMAGE_CAPTURE = 0;
Uri u;
// int z=0;
String z = null;
byte b[];
String largeImagePath = "";
Uri uriLargeImage;
Uri uriThumbnailImage;
Cursor myCursor, cursor1;
int flagg = 0;
// --------------------------------------------
String FILENAME = "http://animsinc.com/test.php";
// NAME = "http://animsinc.com/upload_image.php";
private CarHelper dbcarhelper = null;
private Cursor ourCursor, cur;
String fuel[] = new String[] { "Select Any", "Petrol", "Diesel", "CNG" };
String owner[] = new String[] { "Select Any", "1 Owner", "2 Owners",
"3 Owners", "4 Owners" };
String curloc[] = new String[] { "Select Any", "Mumbai", "Pune", "Thane",
"Delhi" };
Button img, bt, af;
ImageView imgview;
PopupWindow popUp;
EditText et1, et2, et4, et5, et6;
String str1, str2, str4, str5, str6, s1, s2, s3, s4, s5, s6, s7, c1, s, i;
Spinner sp1, sp2, sp3, sp4, sp5, sp6, sp7;
CheckBox cb;
private EditText mDateDisplay;
public static String id = null;
private EditText mDateDisplayone;
private EditText mDateDisplaytwo;
private int mYear;
private int mMonth;
private int mDay;
static final int DATE_DIALOG_ID = 1;
static final int DATE_DIALOG_ID_ONE = 2;
static final int DATE_DIALOG_ID_TWO = 3;
String mm = "";
int flag = 0;
String bal;
public void setvalues(String xID) {
// TODO Auto-generated method stub
id = xID;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_car);
Typeface tf1 = Typeface.createFromAsset(getAssets(), fontPath1);
TextView t1 = (TextView) findViewById(R.id.textView1);
TextView t2 = (TextView) findViewById(R.id.textView2);
TextView t3 = (TextView) findViewById(R.id.textView3);
TextView t4 = (TextView) findViewById(R.id.textView4);
TextView t5 = (TextView) findViewById(R.id.textView5);
TextView t6 = (TextView) findViewById(R.id.textView6);
TextView t7 = (TextView) findViewById(R.id.textView7);
TextView t8 = (TextView) findViewById(R.id.textView8);
TextView t9 = (TextView) findViewById(R.id.textView9);
TextView t10 = (TextView) findViewById(R.id.textView10);
TextView t11 = (TextView) findViewById(R.id.textView11);
TextView t12 = (TextView) findViewById(R.id.textView12);
t1.setTypeface(tf1);
t2.setTypeface(tf1);
t3.setTypeface(tf1);
t4.setTypeface(tf1);
t5.setTypeface(tf1);
t6.setTypeface(tf1);
t7.setTypeface(tf1);
t8.setTypeface(tf1);
t9.setTypeface(tf1);
t10.setTypeface(tf1);
t11.setTypeface(tf1);
t12.setTypeface(tf1);
img = (Button) findViewById(R.id.button3);
// --------------------------------
if (savedInstanceState != null) {
Bitmap carpic;
String B1 = savedInstanceState.getString("message");
// Toast.makeText(this, "SavedYeah"+B1, Toast.LENGTH_LONG).show();
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = OG;
carpic = BitmapFactory.decodeFile((B1), opts);
System.gc();
if (carpic != null) {
imageCam(carpic);
}
}
// --------------------------------
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
imgview = (ImageView) this.findViewById(R.id.imageView1);
img.setOnClickListener(click_img);
Toast.makeText(getApplicationContext(), "bal=" + bal, Toast.LENGTH_LONG)
.show();
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
et4 = (EditText) findViewById(R.id.editText4);
et5 = (EditText) findViewById(R.id.editText5);
et6 = (EditText) findViewById(R.id.editText6);
sp1 = (Spinner) findViewById(R.id.spinner1);
sp1.setOnItemSelectedListener(this);
sp2 = (Spinner) findViewById(R.id.spinner2);
sp2.setOnItemSelectedListener(this);
sp3 = (Spinner) findViewById(R.id.spinner3);
// sp3.setOnItemSelectedListener(this);
sp4 = (Spinner) findViewById(R.id.spinner4);
@SuppressWarnings("unchecked")
ArrayAdapter spnfuel = new ArrayAdapter(this, R.layout.spin_adptr, fuel);
spnfuel.setDropDownViewResource(R.layout.spin_drop);
sp4.setAdapter(spnfuel);
// sp4.setOnItemSelectedListener(this);
sp5 = (Spinner) findViewById(R.id.spinner5);
@SuppressWarnings("unchecked")
ArrayAdapter spncurloc = new ArrayAdapter(this, R.layout.spin_adptr,
curloc);
spncurloc.setDropDownViewResource(R.layout.spin_drop);
sp5.setAdapter(spncurloc);
// sp5.setOnItemSelectedListener(this);
sp6 = (Spinner) findViewById(R.id.spinner6);
@SuppressWarnings("unchecked")
ArrayAdapter spnowner = new ArrayAdapter(this, R.layout.spin_adptr,
owner);
spnowner.setDropDownViewResource(R.layout.spin_drop);
sp6.setAdapter(spnowner);
// sp6.setOnItemSelectedListener(this);
sp7 = (Spinner) findViewById(R.id.spinner7);
@SuppressWarnings("unchecked")
ArrayAdapter spnreg = new ArrayAdapter(this, R.layout.spin_adptr,
curloc);
spnreg.setDropDownViewResource(R.layout.spin_drop);
sp7.setAdapter(spnreg);
af = (Button) findViewById(R.id.img);
//af.setTypeface(tf1);
af.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
flag = 1;
if ((et1.getText().length() == 0)
|| (et2.getText().length() == 0)
|| (et4.getText().length() == 0)
|| (et5.getText().length() == 0)
|| (et6.getText().length() == 0)) {
} else {
if (cb.isChecked()) {
c1 = cb.getText().toString();
// Toast.makeText(getApplicationContext(), "c=" +
// c,Toast.LENGTH_LONG).show();
} else {
c1 = "Not Negotiable";
}
// spinner values
s1 = sp1.getSelectedItem().toString();
s2 = sp2.getSelectedItem().toString();
s3 = sp3.getSelectedItem().toString();
s4 = sp4.getSelectedItem().toString();
s5 = sp5.getSelectedItem().toString();
s6 = sp6.getSelectedItem().toString();
s7 = sp7.getSelectedItem().toString();
// edittext values
str1 = et1.getText().toString();
str2 = et2.getText().toString();
str4 = et4.getText().toString();
str5 = et5.getText().toString();
str6 = et6.getText().toString();
Toast.makeText(
getApplicationContext(),
s1 + s2 + s3 + s4 + s5 + s6 + s7 + str1 + str2
+ str4 + str5 + str6 + c1,
Toast.LENGTH_LONG).show();
}
}
});
dbcarhelper = new CarHelper(this);
dbcarhelper.createDatabase();
dbcarhelper.openDataBase();
if (s != null) {
startManagingCursor(ourCursor);
}
// Toast.makeText(getApplicationContext(), s ,
// Toast.LENGTH_LONG).show();
dbcarhelper.disp();
CarHelper h = new CarHelper(getApplicationContext());
List<String> lab = h.getAllLab();
ArrayAdapter<String> dataAdap = new ArrayAdapter<String>(this,
R.layout.spin_adptr, lab);
dataAdap.setDropDownViewResource(R.layout.spin_drop);
sp1.setAdapter(dataAdap);
sp1.setOnItemSelectedListener(this);
if (i != null) {
startManagingCursor(cur);
}
// Toast.makeText(getApplicationContext(), i ,
// Toast.LENGTH_LONG).show();
sp2.setOnItemSelectedListener(this);
cb = (CheckBox) findViewById(R.id.checkBox1);
//cb.setTypeface(tf1);
Button pickDate = (Button) findViewById(R.id.button4);
//pickDate.setTypeface(tf1);
pickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
updateDisplay();
bt = (Button) findViewById(R.id.button2);
//bt.setTypeface(tf1);
bt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if ((et1.getText().length() == 0)
|| (et2.getText().length() == 0)
|| (et4.getText().length() == 0)
|| (et5.getText().length() == 0)
|| (et6.getText().length() == 0)) {
Toast.makeText(getApplicationContext(),
"Please enter all the details", Toast.LENGTH_LONG)
.show();
} else {
if (cb.isChecked()) {
c1 = cb.getText().toString();
// Toast.makeText(getApplicationContext(), "c=" +
// c,Toast.LENGTH_LONG).show();
} else {
c1 = "Not Negotiable";
}
// spinner values
s1 = sp1.getSelectedItem().toString();
s2 = sp2.getSelectedItem().toString();
s3 = sp3.getSelectedItem().toString();
s4 = sp4.getSelectedItem().toString();
s5 = sp5.getSelectedItem().toString();
s6 = sp6.getSelectedItem().toString();
s7 = sp7.getSelectedItem().toString();
// edittext values
str1 = et1.getText().toString();
// Toast.makeText(getApplicationContext(), "km=" +
// str1,Toast.LENGTH_LONG).show();
str2 = et2.getText().toString();
str4 = et4.getText().toString();
str5 = et5.getText().toString();
str6 = et6.getText().toString();
startDownload();
et1.setText("");
et2.setText("");
et4.setText("");
et5.setText("");
et6.setText("");
sp1.setSelection(0);
sp2.setSelection(0);
sp3.setSelection(0);
sp4.setSelection(0);
sp5.setSelection(0);
sp6.setSelection(0);
sp7.setSelection(0);
}
}
});
}
public void imageCam(Bitmap thumbnail) {
flag = 1;
Bitmap photo = thumbnail;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 70, bos);
b = bos.toByteArray();
bal = Base64.encodeBytes(b);
ImageView imageview = (ImageView) findViewById(R.id.imageView1);
String BX1 = android.os.Build.MANUFACTURER;
if (BX1.equalsIgnoreCase("samsung") && flagg == 0) {
// Toast.makeText(getApplicationContext(), "Device man"+BX1,
// Toast.LENGTH_LONG).show();
Matrix matrix = new Matrix();
matrix.postRotate(90);
photo = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(),
photo.getHeight(), matrix, true);
}
// Bitmap bt=Bitmap.createScaledBitmap(photo, 200, 400, false);
imageview.setImageBitmap(photo);
}
private void startDownload() {
new AddTask().execute(FILENAME);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
}
private View.OnClickListener click_img = new View.OnClickListener() {
@Override
public void onClick(View v) {
openNewGameDialog();
}
};
String[] B = { "Camera", "Gallery" };
private void openNewGameDialog() {
new AlertDialog.Builder(this).setItems(B,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
if (i == 0) {
String BX1 = android.os.Build.MANUFACTURER;
if (BX1.equalsIgnoreCase("samsung")) {
// Toast.makeText(getApplicationContext(),
// "Device man"+BX1, Toast.LENGTH_LONG).show();
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,
CAMERA_IMAGE_CAPTURE);
} else {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent,
CAMERA_PIC_REQUEST);
}
}
else if (i == 1) {
// Intent in = new
// Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// startActivityForResult(in, RESULT_LOAD_IMAGE);
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
}
}).show();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_IMAGE_CAPTURE
&& resultCode == Activity.RESULT_OK) {
flagg = 0;
// Describe the columns you'd like to have returned. Selecting from
// the Thumbnails location gives you both the Thumbnail Image ID, as
// well as the original image ID
String[] projection = {
MediaStore.Images.Thumbnails._ID, // The columns we want
MediaStore.Images.Thumbnails.IMAGE_ID,
MediaStore.Images.Thumbnails.KIND,
MediaStore.Images.Thumbnails.DATA };
String selection = MediaStore.Images.Thumbnails.KIND + "=" + // Select
// only
// mini's
MediaStore.Images.Thumbnails.MINI_KIND;
String sort = MediaStore.Images.Thumbnails._ID + " DESC";
// At the moment, this is a bit of a hack, as I'm returning ALL
// images, and just taking the latest one. There is a better way to
// narrow this down I think with a WHERE clause which is currently
// the selection variable
myCursor = this.managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, selection, null, sort);
long imageId = 0l;
long thumbnailImageId = 0l;
String thumbnailPath = "";
try {
myCursor.moveToFirst();
imageId = myCursor
.getLong(myCursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));
thumbnailImageId = myCursor
.getLong(myCursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
thumbnailPath = myCursor
.getString(myCursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
} finally {
// myCursor.close();
}
// Create new Cursor to obtain the file Path for the large image
String[] largeFileProjection = {
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA };
String largeFileSort = MediaStore.Images.ImageColumns._ID + " DESC";
myCursor = this.managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
largeFileProjection, null, null, largeFileSort);
largeImagePath = "";
try {
myCursor.moveToFirst();
// This will actually give yo uthe file path location of the
// image.
largeImagePath = myCursor
.getString(myCursor
.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
} finally {
// myCursor.close();
}
// These are the two URI's you'll be interested in. They give you a
// handle to the actual images
uriLargeImage = Uri.withAppendedPath(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
String.valueOf(imageId));
uriThumbnailImage = Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
String.valueOf(thumbnailImageId));
// I've left out the remaining code, as all I do is assign the URI's
// to my own objects anyways...
// Toast.makeText(this, ""+largeImagePath,
// Toast.LENGTH_LONG).show();
// Toast.makeText(this, ""+uriLargeImage, Toast.LENGTH_LONG).show();
// Toast.makeText(this, ""+uriThumbnailImage,
// Toast.LENGTH_LONG).show();
if (largeImagePath != null) {
// Toast.makeText(this, "LARGE YES"+largeImagePath,
// Toast.LENGTH_LONG).show();
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = OG;
// thumbnail = (BitmapFactory.decodeFile(picturePath));
thumbnail = BitmapFactory.decodeFile((largeImagePath), opts);
System.gc();
if (thumbnail != null) {
// Toast.makeText(this, "Try Without Saved Instance",
// Toast.LENGTH_LONG).show();
imageCam(thumbnail);
}
}
if (uriLargeImage != null) {
// Toast.makeText(this, ""+uriLargeImage,
// Toast.LENGTH_LONG).show();
}
if (uriThumbnailImage != null) {
// Toast.makeText(this, ""+uriThumbnailImage,
// Toast.LENGTH_LONG).show();
}
}
if (requestCode == 1337 && resultCode == RESULT_OK) {
flagg = 0;
Bundle extras = data.getExtras();
// if (extras!=null){
if (extras.keySet().contains("data")) {
BitmapFactory.Options options = new BitmapFactory.Options();
// options.inSampleSize = 1;
// options.inPurgeable = true;
// options.inInputShareable = true;
thumbnail = (Bitmap) extras.get("data");
// image(thumbnail);
if (thumbnail != null) {
// Toast.makeText(this, "YES Thumbnail",
// Toast.LENGTH_LONG).show();
BitmapFactory.Options opt = new BitmapFactory.Options();
// options.inSampleSize = 1;
// options.inPurgeable = true;
// options.inInputShareable = true;
thumbnail = (Bitmap) extras.get("data");
imageCam(thumbnail);
}
} else {
Uri imageURI = getIntent().getData();
ImageView imageview = (ImageView) findViewById(R.id.imageView1);
imageview.setImageURI(imageURI);
if (imageURI != null) {
// Toast.makeText(this, "YES Image Uri",
// Toast.LENGTH_LONG).show();
}
// Toast.makeText(CreateProfile.this, "Picture NOt taken",
// Toast.LENGTH_LONG).show();
}
}
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
flagg = 1;
String[] filePathColumn = { MediaStore.Images.Media.DATA };
cursor1 = getContentResolver().query(selectedImage, filePathColumn,
null, null, null);
cursor1.moveToFirst();
int columnIndex = cursor1.getColumnIndex(filePathColumn[0]);
String picturePath = cursor1.getString(columnIndex);
// cursor.close();
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = OG;
// thumbnail = (BitmapFactory.decodeFile(picturePath));
thumbnail = BitmapFactory.decodeFile((picturePath), opts);
System.gc();
if (thumbnail != null) {
imageCam(thumbnail);
} else {
Toast.makeText(this, "Please select a different picture",
Toast.LENGTH_LONG).show();
}
}
else {
// imgview.setBackgroundResource(R.drawable.bbtb);
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View position, int arg2,
long arg3) {
int id = parent.getId();
switch (id) {
case R.id.spinner1:
String b = sp1.getSelectedItem().toString();
if (b != null) {
dbcarhelper.m_pop(b);
}
CarHelper ch = new CarHelper(getApplicationContext());
List<String> lables = ch.getAllLabels();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
R.layout.spin_adptr, lables);
dataAdapter.setDropDownViewResource(R.layout.spin_drop);
sp2.setAdapter(dataAdapter);
break;
case R.id.spinner2:
String a = sp2.getSelectedItem().toString();
if (a != null) {
dbcarhelper.v_pop(a);
}
CarHelper c = new CarHelper(getApplicationContext());
List<String> lable = c.getAllLabel();
ArrayAdapter<String> dAdapter = new ArrayAdapter<String>(this,
R.layout.spin_adptr, lable);
dAdapter.setDropDownViewResource(R.layout.spin_drop);
sp3.setAdapter(dAdapter);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
mDay);
case DATE_DIALOG_ID_ONE:
return new DatePickerDialog(this, mDateSetListenerone, mYear,
mMonth, mDay);
case DATE_DIALOG_ID_TWO:
return new DatePickerDialog(this, mDateSetListenertwo, mYear,
mMonth, mDay);
}
return null;
}
private DatePickerDialog.OnDateSetListener mDateSetListenertwo = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplaytwo();
}
};
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
break;
case DATE_DIALOG_ID_ONE:
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
break;
case DATE_DIALOG_ID_TWO:
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
break;
}
}
private void updateDisplaytwo() {
int month = mMonth + 1;
if (month == 1) {
mm = "Jan";
} else if (month == 2) {
mm = "Feb";
} else if (month == 3) {
mm = "Mar";
} else if (month == 4) {
mm = "Apr";
} else if (month == 5) {
mm = "May";
} else if (month == 6) {
mm = "Jun";
}
else if (month == 7) {
mm = "Jul";
} else if (month == 8) {
mm = "Aug";
} else if (month == 9) {
mm = "Sep";
} else if (month == 10) {
mm = "Oct";
}
else if (month == 11) {
mm = "Nov";
}
else if (month == 12) {
mm = "Dec";
}
mDateDisplaytwo.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mDay).append("-")
// .append(mMonth + 1).append("-")
.append(mm).append("-")
.append(mYear).append(""));
}
private void updateDisplay() {
int month = mMonth + 1;
if (month == 1) {
mm = "Jan";
} else if (month == 2) {
mm = "Feb";
} else if (month == 3) {
mm = "Mar";
} else if (month == 4) {
mm = "Apr";
} else if (month == 5) {
mm = "May";
} else if (month == 6) {
mm = "Jun";
}
else if (month == 7) {
mm = "Jul";
} else if (month == 8) {
mm = "Aug";
} else if (month == 9) {
mm = "Sep";
} else if (month == 10) {
mm = "Oct";
}
else if (month == 11) {
mm = "Nov";
}
else if (month == 12) {
mm = "Dec";
}
et5.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mDay).append("-")
// .append(mMonth + 1).append("-")
.append(mm).append("-")
.append(mYear).append(""));
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
private DatePickerDialog.OnDateSetListener mDateSetListenerone = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplayone();
}
};
private void updateDisplayone() {
int month = mMonth + 1;
if (month == 1) {
mm = "Jan";
} else if (month == 2) {
mm = "Feb";
} else if (month == 3) {
mm = "Mar";
} else if (month == 4) {
mm = "Apr";
} else if (month == 5) {
mm = "May";
} else if (month == 6) {
mm = "Jun";
}
else if (month == 7) {
mm = "Jul";
} else if (month == 8) {
mm = "Aug";
} else if (month == 9) {
mm = "Sep";
} else if (month == 10) {
mm = "Oct";
}
else if (month == 11) {
mm = "Nov";
}
else if (month == 12) {
mm = "Dec";
}
mDateDisplayone.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mDay).append("-")
// .append(mMonth + 1).append("-")
.append(mm).append("-")
.append(mYear).append(""));
}
public class AddTask extends AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(Add.this, "Result=" + result + "cnt= " + cnt, 1000).show();
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
@Override
protected String doInBackground(String... params) {
String is = null;
String trans = "No", ac = "No", lockk = "No", roof = "No", win = "No", seat = "No", stearing = "No", player = "No", wheel = "No", sys = "No", dr = "No";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://animsinc.com/test.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
30);
nameValuePairs.add(new BasicNameValuePair("Make", s1));
nameValuePairs.add(new BasicNameValuePair("Model", s2));
nameValuePairs.add(new BasicNameValuePair("Version", s3));
nameValuePairs.add(new BasicNameValuePair("FuelType", s4));
nameValuePairs.add(new BasicNameValuePair("Kilo", str1));
nameValuePairs.add(new BasicNameValuePair("Price", str2));
nameValuePairs.add(new BasicNameValuePair("Reg", s7));
nameValuePairs.add(new BasicNameValuePair("Color", str4));
nameValuePairs.add(new BasicNameValuePair("Mdate", str5));
nameValuePairs.add(new BasicNameValuePair("Desc", str6));
nameValuePairs.add(new BasicNameValuePair("Loc", s5));
nameValuePairs.add(new BasicNameValuePair("Owners", s6));
nameValuePairs.add(new BasicNameValuePair("Negot", c1));
nameValuePairs.add(new BasicNameValuePair("Trans", trans));
nameValuePairs.add(new BasicNameValuePair("Drive", dr));
nameValuePairs.add(new BasicNameValuePair("Lockk", lockk));
nameValuePairs.add(new BasicNameValuePair("AC", ac));
nameValuePairs.add(new BasicNameValuePair("Window", win));
nameValuePairs
.add(new BasicNameValuePair("Stearing", stearing));
nameValuePairs.add(new BasicNameValuePair("Seats", seat));
nameValuePairs.add(new BasicNameValuePair("Player", player));
nameValuePairs.add(new BasicNameValuePair("Sunroof", roof));
nameValuePairs.add(new BasicNameValuePair("Sound", sys));
nameValuePairs.add(new BasicNameValuePair("Wheel", wheel));
nameValuePairs.add(new BasicNameValuePair("Seller_ID", id));
nameValuePairs.add(new BasicNameValuePair("Image", bal));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
cnt++;
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = EntityUtils.toString(entity);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
}
return Integer.toString(cnt);
}
}
}
//这是我的PHP代码test.php
<?php
require 'DbConnect.php';
$Make = $_POST["Make"];
$Model = $_POST["Model"];
$Version= $_POST["Version"];
$FuelType= $_POST["FuelType"];
$Kilo = $_POST["Kilo"];
$Price= $_POST["Price"];
$Reg= $_POST["Reg"];
$Color= $_POST["Color"];
$Mdate= $_POST["Mdate"];
$Desc= $_POST["Desc"];
$Loc= $_POST["Loc"];
$Owners = $_POST["Owners"];
$Negot= $_POST["Negot"];
$Trans= $_POST["Trans"];
$AC= $_POST["AC"];
$car_lockk= $_POST["Lockk"];
$Sunroof= $_POST["Sunroof"];
$Window= $_POST["Window"];
$Seat= $_POST["Seats"];
$Stearing= $_POST["Stearing"];
$Music= $_POST["Player"];
$Wheels= $_POST["Wheel"];
$Sound= $_POST["Sound"];
$Drive= $_POST["Drive"];
$IMG= $_POST["Image"];
$ID = $_POST["Seller_ID"];
$query2 = "INSERT INTO used_cars (make, model, version, color, manufacturing_date, km_driven, fuel_type, expected_price, negotiable, registration_place, no_of_owners, description, current_location, transmission, ac, sunroof, window, seats, stearing, player, wheels, sound_system, drive, car_lockk, seller_id, img) VALUES ('$Make', '$Model', '$Version', '$Color', '$Mdate', '$Kilo', '$FuelType', '$Price', '$Negot', '$Reg', '$Owners', '$Desc', '$Loc', '$Trans', '$AC', '$Sunroof', '$Window', '$Seat', '$Stearing', '$Music', '$Wheels', '$Sound', '$Drive', '$car_lockk', '$ID', '$IMG')";
if((mysql_query($query2)))
{
echo 'Success';
}else
{
echo 'Fail';
}
?>
//这是我的DBConnect.php
<?php
$mysql_host='localhost';
$mysql_user='abc';
$mysql_pass ='abc';
if(!(@mysql_connect ($mysql_host, $mysql_user, $mysql_pass)) ||!(@mysql_select_db('animsinc_CarList')) ){
die('Not Connected');
}else{
//echo 'Connected.';
}
?>
答案 0 :(得分:1)
可能需要修改此代码..
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
cnt++;
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
两次使用httpclient.execute()可能是数据被插入两次的可能原因。
像这样修改你的代码..
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// httpclient.execute(httppost);
cnt++;
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
答案 1 :(得分:0)
实际上这种类型的问题一般都是我面临的浏览器问题...... 要修复它,您可以尝试编辑.htaccess以防止在浏览器请求时重定向到索引文件 我猜你的查询运行正常,但在服务器中部署时会出现问题 为了避免这种情况,请在启动时将其添加到您的php文件中,
if(strpos($_SERVER['HTTP_USER_AGENT'],'Mediapartners-Google') !== false) {
exit();
}
这可以帮到你