۱۳۹۵/۰۶/۲۹, ۰۴:۱۶ ب.ظ
با سلام و درود
من یک برنامه ساختم که از درون اون مقادیر edittext ها رو به دیتابیس سرور ارسال می کنه ولی در دیتابیس به صورت ؟؟؟؟؟ نمایش داده می شود
کد php
کد جاوا
من یک برنامه ساختم که از درون اون مقادیر edittext ها رو به دیتابیس سرور ارسال می کنه ولی در دیتابیس به صورت ؟؟؟؟؟ نمایش داده می شود
کد php
کد پیاچپی:
<?php
require_once("include.php");
$error = array();
if( isset( $_POST['title'] ) && ( !empty( $_POST['title'] ) ) &&
isset( $_POST['intro'] ) && ( !empty( $_POST['intro'] ) ) &&
isset( $_POST['desc'] ) && ( !empty( $_POST['desc'] ) ) &&
isset( $_POST['seller'] ) && ( !empty( $_POST['seller'] ) ) &&
isset( $_POST['email'] ) && ( !empty( $_POST['email'] ) ) &&
isset( $_POST['phone'] ) && ( !empty( $_POST['phone'] ) ) &&
isset( $_POST['cat'] ) && ( !empty( $_POST['cat'] ) ) &&
isset( $_POST['image'] ) && ( !empty( $_POST['image'] ) ) )
{
$title = $_POST['title'];
$intro = $_POST['intro'];
$desc = $_POST['desc'];
$seller= $_POST['seller'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$cat = $_POST['cat'];
$image = $_POST['image'];
$decodedImage = base64_decode( $image );
$location = "img/" . $title . "_" . rand( rand(5 , 50) , rand( 500 , 900 ) ) .
"_" . date("i") . "_" . date("d-m-Y") . ".jpg";
$resultOfCreatingImage = file_put_contents( $location , $decodedImage );
if( $resultOfCreatingImage == false )
{
$error['error'] = "failure_creating_image";
}
else
{
$query= "INSERT INTO ads(title, intro, description, image, seller, email, phone, cat_id) " .
"VALUES('".$title."', '".$intro."', '".$desc."', '".$location."', '".$seller.
"', '".$email."', '".$phone."', '".$cat."')";
$connect = @mysqli_connect( $hostname , $username , $password , $database );
if( $connect )
{
@mysqli_query( $connect , "SET CHARACTER SET utf8;" );
@mysqli_query( $connect , $query );
if( @mysqli_affected_rows( $connect ) > 0 )
{
$error['error'] = "done";
}
else
{
@unlink( $location );
$error['error'] = "failure_inserting_database!";
}
}
else
{
@unlink( $location );
$error['error'] = "failure_connecting_database";
}
}
}
else
{
$error['error'] = "failure_post";
}
die ( json_encode( $error['error'] ) );
?>
کد جاوا
کد پیاچپی:
public class insert_ads extends AppCompatActivity
{
private String url_insert_ads;
private String[] id;
private String[] name;
private ListView lv_cat;
private EditText title , intro , desc , seller , email , phone;
private TextView selected_cat , selected_img_txt;
private ImageView selected_img;
private String[] selected_cat_info = new String[2];
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insert_ads);
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
/*----------------------------------------------------------*/
selected_cat = (TextView) findViewById( R.id.txt_insert_ads_selected_cat );
selected_img_txt = (TextView) findViewById( R.id.insert_ads_selected_img_txt );
selected_img = (ImageView) findViewById( R.id.insert_ads_selected_img );
title = (EditText) findViewById( R.id.insert_ads_title );
intro = (EditText) findViewById( R.id.insert_ads_intro );
desc = (EditText) findViewById( R.id.insert_ads_desc );
seller = (EditText) findViewById( R.id.insert_ads_seller );
email = (EditText) findViewById( R.id.insert_ads_email );
phone = (EditText) findViewById( R.id.insert_ads_phone );
/*----------------------------------------------------------*/
Bundle data = getIntent().getExtras();
url_insert_ads = data.getString( "url" );
id = data.getStringArray("cat_id");
name = data.getStringArray("cat_name");
ArrayList<String> cats = new ArrayList<>();
for (int i = 0; i < id.length ; i++)
{
cats.add( i , name[i] );
}
ArrayAdapter<String> adb = new ArrayAdapter<String>(
this , android.R.layout.simple_list_item_1 , cats
);
lv_cat = (ListView) findViewById( R.id.insert_ads_cat_list );
lv_cat.setAdapter(adb);
lv_cat.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long _id) {
selected_cat_info[0] = id[position];
selected_cat_info[1] = name[position];
selected_cat.setText(name[position]);
}
}
);
}
private int my_requestCode_gallery = 2;
public void onBtnGalleryClick( View v )
{
Intent i = new Intent( Intent.ACTION_PICK ,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
);
startActivityForResult( i , my_requestCode_gallery );
}
private int my_requestCode = 1;
private Bitmap my_bitmap;
private String my_final_image;
public void onBtnCameraClick( View v )
{
if( getPackageManager().hasSystemFeature( PackageManager.FEATURE_CAMERA_ANY ) )
{
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, my_requestCode);
}
else
{
Toast.makeText( getApplicationContext() ,
getString( R.string.no_camera_error ) ,
Toast.LENGTH_LONG ).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if( requestCode == my_requestCode && resultCode == RESULT_OK )
{
Bundle e = data.getExtras();
my_bitmap = (Bitmap) e.get( "data" );
show_captured_image();
}
else if( requestCode == my_requestCode_gallery && resultCode == RESULT_OK )
{
Uri image = data.getData();
show_internal_image( image );
}
else
{
Toast.makeText( getApplicationContext() ,
getString( R.string.get_image_error ) ,
Toast.LENGTH_LONG ).show();
}
}
public void show_captured_image()
{
try
{
AlertDialog.Builder imageLoader = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.full_screen_image,
(ViewGroup) findViewById(R.id.full_img_layout_root));
ImageView bigImage = (ImageView) layout.findViewById(R.id.full_img_img);
bigImage.setImageBitmap(my_bitmap);
TextView imgTitle = (TextView) layout.findViewById(R.id.full_img_title);
imgTitle.setText(R.string.captured_img_title);
imageLoader.setView(layout);
imageLoader.setCancelable(false);
imageLoader.setPositiveButton(R.string.captured_img_btn_ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
selected_img.setImageBitmap( my_bitmap );
selected_img_txt.setText( R.string.captured_img_is_true );
dialog.dismiss();
}
}
);
imageLoader.setNegativeButton(R.string.captured_img_btn_again,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
onBtnCameraClick(null);
}
}
);
imageLoader.setNeutralButton(R.string.btn_Back_to_home,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
);
imageLoader.create();
imageLoader.show();
}
catch ( Exception e )
{
/*
* Log.i( "MatiMessage" , "error 1 -> " + e.toString() );
*/
}
}
public void show_internal_image( final Uri imageUri )
{
try
{
AlertDialog.Builder imageLoader = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.full_screen_image,
(ViewGroup) findViewById(R.id.full_img_layout_root));
ImageView bigImage = (ImageView) layout.findViewById(R.id.full_img_img);
bigImage.setImageURI(imageUri);
TextView imgTitle = (TextView) layout.findViewById(R.id.full_img_title);
imgTitle.setText(R.string.captured_img_title);
imageLoader.setView(layout);
imageLoader.setCancelable(false);
imageLoader.setPositiveButton(R.string.captured_img_btn_ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
selected_img.setImageURI(imageUri);
selected_img_txt.setText( R.string.captured_img_is_true );
dialog.dismiss();
}
}
);
imageLoader.setNegativeButton(R.string.captured_img_btn_again,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
onBtnGalleryClick(null);
}
}
);
imageLoader.setNeutralButton(R.string.btn_Back_to_home,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
);
imageLoader.create();
imageLoader.show();
}
catch ( Exception e )
{
/*
* Log.i( "MatiMessage" , "error 1 -> " + e.toString() );
*/
}
}
public void onBtnInsertAdsClick( View v )
{
if( title.getText().length() > 1 )
{
if( intro.getText().length() > 1 )
{
if( desc.getText().length() > 1 )
{
if( seller.getText().length() > 1 )
{
if( email.getText().length() > 1 )
{
if( phone.getText().length() > 1 )
{
if( selected_cat.getText() != getString( R.string.insert_ads_form_sel_cat_text ) )
{
if( selected_img_txt.getText() != getString( R.string.insert_ads_form_sel_cat_text ) )
{
Bitmap image = ( (BitmapDrawable) selected_img.getDrawable() ).getBitmap();
HashMap<String , String> my_data = new HashMap<>();
my_data.put( "title" , title.getText().toString() );
my_data.put( "intro" , intro.getText().toString() );
my_data.put( "desc" , desc.getText().toString() );
my_data.put("seller", seller.getText().toString());
my_data.put("email", email.getText().toString());
my_data.put("phone", phone.getText().toString());
my_data.put("cat", selected_cat_info[0]);
UploadImage upload = new UploadImage( my_data , image , this );
upload.execute();
}
else
{
Toast.makeText( getApplicationContext() ,
getString( R.string.insert_ads_form_error_sel_img ) ,
Toast.LENGTH_LONG ).show();
}
}
else
{
Toast.makeText( getApplicationContext() ,
getString( R.string.insert_ads_form_error_sel_cat ) ,
Toast.LENGTH_LONG ).show();
}
}
else
{
phone.setHint(R.string.insert_ads_form_error_hint);
}
}
else
{
email.setHint( R.string.insert_ads_form_error_hint );
}
}
else
{
seller.setHint( R.string.insert_ads_form_error_hint );
}
}
else
{
desc.setHint( R.string.insert_ads_form_error_hint );
}
}
else
{
intro.setHint( R.string.insert_ads_form_error_hint );
}
}
else
{
title.setHint( R.string.insert_ads_form_error_hint );
}
}
private class UploadImage extends AsyncTask<Void , Void , Boolean>
{
private Context main_con;
private HashMap<String , String> main_hm;
private Bitmap main_image;
public UploadImage( HashMap<String , String> hm , Bitmap image , Context con )
{
main_hm = hm;
main_image = image;
main_con = con;
}
@Override
protected Boolean doInBackground(Void... params)
{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
main_image.compress( Bitmap.CompressFormat.JPEG , 100 , outStream );
String encodedImage = Base64.encodeToString(
outStream.toByteArray() , Base64.DEFAULT
);
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add( new BasicNameValuePair("title" , main_hm.get("title") ) );
dataToSend.add( new BasicNameValuePair("intro" , main_hm.get("intro") ) );
dataToSend.add( new BasicNameValuePair("desc" , main_hm.get("desc") ) );
dataToSend.add( new BasicNameValuePair("seller" , main_hm.get("seller") ) );
dataToSend.add( new BasicNameValuePair("email" , main_hm.get("email") ) );
dataToSend.add( new BasicNameValuePair("phone" , main_hm.get("phone") ) );
dataToSend.add( new BasicNameValuePair("cat" , main_hm.get("cat") ) );
dataToSend.add(new BasicNameValuePair("image", encodedImage));
HttpParams myHttpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(myHttpParams, 1000 * 30);
HttpConnectionParams.setSoTimeout(myHttpParams, 1000 * 30);
HttpClient client = new DefaultHttpClient( myHttpParams );
HttpPost post = new HttpPost( url_insert_ads );
try {
post.setEntity( new UrlEncodedFormEntity( dataToSend ));
client.execute( post );
return true;
} catch ( Exception e ) {
/*
* Log.i( "MatiMessage" , "error in posting data -> " + e.toString() );
*/
return false;
}
}
@Override
protected void onPostExecute(Boolean result)
{
String message = "";
if( result == true )
{
message = getString( R.string.result_of_insert_message_success );
}
else
{
message = getString( R.string.result_of_insert_message_failure );
}
AlertDialog.Builder alert = new AlertDialog.Builder( main_con );
alert.setCancelable(false);
alert.setTitle(R.string.result_of_insert_title);
alert.setMessage(message);
alert.setPositiveButton(R.string.btn_Back_to_home,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
);
alert.create();
alert.show();
}
}
public void onBtnBackClick( View v )
{
finish();
}
}
یه آدم معمولی که به برنامه نویسی علاقه داره