Guest User

Untitled

a guest
Apr 9th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.05 KB | None | 0 0
  1. import android.app.ProgressDialog;
  2. import android.content.Intent;
  3. import android.graphics.drawable.Drawable;
  4. import android.net.Uri;
  5. import android.os.AsyncTask;
  6. import android.os.Bundle;
  7. import android.support.design.widget.TextInputEditText;
  8. import android.support.design.widget.TextInputLayout;
  9. import android.util.Base64;
  10. import android.view.Gravity;
  11. import android.view.Menu;
  12. import android.view.View;
  13. import android.widget.ArrayAdapter;
  14. import android.widget.EditText;
  15. import android.widget.FrameLayout;
  16. import android.widget.ImageButton;
  17. import android.widget.Spinner;
  18. import android.widget.TextView;
  19. import android.os.Bundle;
  20. import android.app.Activity;
  21.  
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.net.URI;
  26. import java.util.ArrayList;
  27.  
  28. import android.media.MediaPlayer;
  29. import android.media.MediaRecorder;
  30. import android.os.Environment;
  31. import android.view.View;
  32. import android.view.View.OnClickListener;
  33. import android.widget.Button;
  34. import android.widget.TextView;
  35. import android.widget.Toast;
  36.  
  37. import static android.R.attr.data;
  38. import static android.support.design.R.id.text;
  39. import static java.security.AccessController.getContext;
  40.  
  41. import android.net.Uri;
  42.  
  43. import org.apache.http.HttpResponse;
  44. import org.apache.http.NameValuePair;
  45. import org.apache.http.client.HttpClient;
  46. import org.apache.http.client.entity.UrlEncodedFormEntity;
  47. import org.apache.http.client.methods.HttpPost;
  48. import org.apache.http.impl.client.DefaultHttpClient;
  49. import org.apache.http.message.BasicNameValuePair;
  50. import org.apache.http.protocol.HTTP;
  51. import org.apache.http.util.EntityUtils;
  52. import org.json.JSONException;
  53. import org.json.JSONObject;
  54.  
  55.  
  56. public class NewAdAcvtivity extends MainActivity {
  57.  
  58. String myUri;
  59.  
  60. Spinner spinner_category;
  61. //تعریف مدیا ریکوردر
  62. Uri uri;
  63. MediaRecorder recorder;
  64. EditText title_text;
  65. TextInputLayout title_layout;
  66. TextInputLayout description_layout;
  67. EditText description_text;
  68. Button submit_bt;
  69. Button StopButtun;
  70. JSONObject new_ad;
  71. @Override
  72. protected void onCreate(final Bundle savedInstanceState) {
  73. super.onCreate(savedInstanceState);
  74.  
  75.  
  76. FrameLayout content_frame = (FrameLayout) findViewById(R.id.content_frame);
  77. getLayoutInflater().inflate(R.layout.new_ad, content_frame);
  78.  
  79.  
  80. navigationView.getMenu().findItem(R.id.mnu_new).setChecked(true);
  81.  
  82. //تعریف اسپینرها
  83. spinner_category = (Spinner) findViewById(R.id.spinner_category);
  84.  
  85.  
  86.  
  87. //تعریغ ادیت تکست عنوان
  88.  
  89. description_layout=(TextInputLayout)findViewById(R.id.description_layout);
  90. description_text=(EditText)findViewById(R.id.description_text);
  91. title_layout=(TextInputLayout)findViewById(R.id.title_layout);
  92. title_text=(EditText)findViewById(R.id.title_text);
  93. title_text.requestFocus();
  94.  
  95. submit_bt=(Button)findViewById(R.id.submit_bt);
  96.  
  97. getSupportActionBar().setTitle("ثبت ");
  98. for (int i = 0; i < toolbar.getChildCount(); i++) {
  99. View view = toolbar.getChildAt(i);
  100. if (view instanceof TextView) {
  101. TextView tv = (TextView) view;
  102. tv.setTextSize(18);
  103. }
  104.  
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  112.  
  113. Drawable arrow = getResources().getDrawable(R.drawable.ic_bck);
  114. getSupportActionBar().setHomeAsUpIndicator(arrow);
  115.  
  116. toolbar.setNavigationOnClickListener(new OnClickListener() {
  117. @Override
  118. public void onClick(View view) {
  119. drawer.closeDrawer(Gravity.RIGHT);
  120. NewAdAcvtivity.this.finish();
  121.  
  122.  
  123. }
  124. });
  125.  
  126.  
  127. ArrayAdapter<CharSequence> myadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.category, R.layout.row);
  128. spinner_category.setAdapter(myadapter);
  129. submit_bt.setOnClickListener(new OnClickListener() {
  130. @Override
  131. public void onClick(View view) {
  132.  
  133.  
  134. //encode to json for phpfile
  135. JSONObject json_o=new JSONObject();
  136. try {
  137.  
  138.  
  139. json_o.put("id", 20);
  140. json_o.put("name", "ali");
  141. String json_text=json_o.toString();
  142.  
  143.  
  144.  
  145.  
  146. JSONObject json_convert=new JSONObject(json_text);
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154. }catch (JSONException e){
  155. e.printStackTrace();
  156. }
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165. boolean is_validate=true;
  166. if(title_text.getText().toString().trim().length()>=5){
  167.  
  168. title_layout.setErrorEnabled(false);
  169.  
  170.  
  171. }else{
  172. title_layout.setError("دوست من حداقل ‍5 کلمه عنوان بده بیاد:|");
  173. title_layout.setErrorEnabled(true);
  174. is_validate=false;
  175. }
  176.  
  177.  
  178. if(description_text.getText().toString().trim().length()>=10){
  179.  
  180. description_layout.setErrorEnabled(false);
  181.  
  182. }else{
  183.  
  184. description_layout.setError("کمه ");
  185. description_layout.setErrorEnabled(true);
  186.  
  187. is_validate=false;
  188.  
  189. }
  190. if(spinner_category.getSelectedItemPosition()==0)
  191. {
  192.  
  193.  
  194. ((TextView)spinner_category.getSelectedView()).setError("");
  195. }
  196.  
  197.  
  198.  
  199. //for upload voice
  200.  
  201. if(is_validate)
  202. {
  203.  
  204. new voice().execute();
  205.  
  206.  
  207.  
  208. new_ad=new JSONObject();
  209.  
  210. try {
  211. new_ad.put("voice",myUri);
  212. new_ad.put("user_id",0);
  213. new_ad.put("title",title_text.getText().toString().trim());
  214. new_ad.put("description",description_text.getText().toString().trim());
  215. new_ad.put("category",spinner_category.getSelectedItemPosition());
  216. new_ad.put("title",title_text.getText().toString().trim());
  217.  
  218. new_ad.put("command","new_ad");
  219. }catch (JSONException e){
  220. e.printStackTrace();
  221. }
  222.  
  223.  
  224. Toast.makeText(getApplicationContext(),new_ad.toString(),Toast.LENGTH_LONG).show();
  225.  
  226. new send_ad().execute();
  227. }
  228. else
  229. {
  230.  
  231. Toast.makeText(getApplicationContext(),"salam",Toast.LENGTH_SHORT).show();
  232. }
  233.  
  234.  
  235. }
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248. });
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255. recorder = new MediaRecorder();
  256. recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  257. recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
  258. recorder.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/myrecording.mp3");
  259. recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
  260.  
  261.  
  262.  
  263. ((Button)findViewById(R.id.StartButton)).setOnClickListener(new OnClickListener() {
  264. @Override
  265. public void onClick(View view) {
  266.  
  267.  
  268.  
  269. try {
  270. Toast.makeText(getApplicationContext(), "ضبط صدا شروع شدD: ",
  271. Toast.LENGTH_LONG).show();
  272.  
  273. recorder.prepare();
  274. recorder.start();
  275.  
  276. }catch (Exception e){}
  277. }
  278.  
  279.  
  280. });
  281.  
  282.  
  283. ((Button)findViewById(R.id.StopButton)).setOnClickListener(new OnClickListener() {
  284. @Override
  285. public void onClick(View view) {
  286. Toast.makeText(getApplicationContext(), "با موفقیت ضبط شد",
  287. Toast.LENGTH_SHORT).show();
  288.  
  289.  
  290. try {
  291.  
  292. recorder.stop();
  293.  
  294. Uri myUri = Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath() + "/myrecording.mp3");
  295.  
  296. }catch (Exception e){}
  297.  
  298.  
  299.  
  300. }
  301. });
  302.  
  303.  
  304.  
  305. }
  306.  
  307.  
  308.  
  309. public class voice extends AsyncTask<Void,Void,String>{
  310. ProgressDialog pd=new ProgressDialog(NewAdAcvtivity.this);
  311.  
  312. protected void onPreExecute(){
  313. super.onPreExecute();
  314. pd.setMessage("داره میاد...");
  315. pd.show();
  316.  
  317.  
  318.  
  319. }
  320.  
  321.  
  322.  
  323. @Override
  324. protected String doInBackground(Void... voids) {
  325. ArrayList<NameValuePair> namevaluepairs=new ArrayList<NameValuePair>();
  326. namevaluepairs.add(new BasicNameValuePair("voice",myUri));
  327.  
  328. try{
  329. HttpClient httpclient=new DefaultHttpClient();
  330. HttpPost httppost=new HttpPost("http://192.168.1.34/command.php");
  331. httppost.setEntity(new UrlEncodedFormEntity(namevaluepairs));
  332. HttpResponse httpresponse=httpclient.execute(httppost);
  333. final String response= EntityUtils.toString(httpresponse.getEntity());
  334. runOnUiThread(new Runnable() {
  335. @Override
  336. public void run() {
  337. Toast.makeText(getBaseContext(),response,Toast.LENGTH_SHORT).show();
  338.  
  339. }
  340. });
  341.  
  342.  
  343. }catch (Exception e){
  344. e.printStackTrace();
  345.  
  346.  
  347.  
  348.  
  349. }
  350.  
  351. return null;
  352. }
  353.  
  354. protected void onPostExecute(String result){
  355. super.onPostExecute(result);
  356. pd.hide();
  357. pd.dismiss();
  358.  
  359. }
  360.  
  361. }
  362. public class send_ad extends AsyncTask<Void,Void,String>{
  363. ProgressDialog pd=new ProgressDialog(NewAdAcvtivity.this);
  364.  
  365.  
  366. protected void onPreExecute ()
  367. {
  368. super.onPreExecute();
  369.  
  370. pd.setMessage("واسا داره میاد");
  371. pd.show();
  372.  
  373. }
  374.  
  375.  
  376. @Override
  377. protected String doInBackground(Void... voids) {
  378. ArrayList<NameValuePair> namevaluepairs =new ArrayList<NameValuePair>();
  379.  
  380. namevaluepairs.add(new BasicNameValuePair("myjson",new_ad.toString()));
  381. try {
  382.  
  383. HttpClient httpclient=new DefaultHttpClient();
  384. HttpPost httppost=new HttpPost("http://192.168.1.34/command.php");
  385. httppost.setEntity(new UrlEncodedFormEntity(namevaluepairs, HTTP.UTF_8));
  386. HttpResponse httpresponse=httpclient.execute(httppost);
  387.  
  388. String response=EntityUtils.toString(httpresponse.getEntity());
  389. if (response.startsWith("<myapp>")&&response.endsWith("<myapp>")){
  390. //response is valid
  391. response =response.replace("<myapp>","").replace("<myapp>","");
  392.  
  393. }else {
  394. final String finalResponse=response;
  395. runOnUiThread(new Runnable() {
  396. @Override
  397. public void run() {
  398. Toast.makeText(getBaseContext(),finalResponse,Toast.LENGTH_LONG).show();
  399. }
  400. });
  401.  
  402.  
  403. }
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410. }catch (Exception e)
  411. {
  412. e.printStackTrace();
  413. }
  414.  
  415.  
  416. return null;
  417. }
  418. protected void onPostExecute(String result){
  419.  
  420. super.onPostExecute(result);
  421. pd.hide();
  422. pd.dismiss();
  423.  
  424.  
  425. }
  426.  
  427.  
  428.  
  429.  
  430. }
  431.  
  432.  
  433.  
  434.  
  435. };
  436.  
  437. <?php
  438.  
  439.  
  440. $post_data=@$_POST['myjson'];
  441.  
  442. $post_data=json_decode($post_data,true);
  443.  
  444. $command=$post_data['command'];
  445.  
  446.  
  447.  
  448.  
  449.  
  450. $server="localhost";
  451. $user="root";
  452. $pass="";
  453. $db="mydb";
  454.  
  455.  
  456. $link=mysqli_connect($server,$user,$pass,$db);
  457.  
  458. mysqli_set_charset($link,"utf8");
  459.  
  460.  
  461.  
  462.  
  463.  
  464. if ($command=="get_contact_details")
  465. {
  466. $user_id=$post_data['user_id'];
  467.  
  468.  
  469.  
  470.  
  471.  
  472. $query="select * from user where id=$user_id";
  473.  
  474. $result=mysqli_query($link,$query);
  475. echo $num=mysqli_num_rows($result);
  476.  
  477.  
  478. if($row=mysqli_fetch_assoc($result))
  479. {
  480. echo "<sheypoor>".json_encode($row)."</sheypoor>";
  481.  
  482. }
  483. else
  484. {
  485.  
  486. echo "<sheypoor>error</sheypoor>";
  487.  
  488. }
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495. exit();
  496. }
  497.  
  498.  
  499. //get_ad_list
  500. if ($command=="get_ad_list")
  501. {
  502.  
  503. $category_filter=$post_data['category_filter'];
  504. $last_ad_id=$post_data['last_ad_id'];
  505. $filter="";
  506.  
  507.  
  508. if($category_filter!==0)
  509. {
  510. $filter="category=".$category_filter;
  511.  
  512. }
  513.  
  514. $filter2="";
  515. if($last_ad_id!=0)
  516. {
  517.  
  518. if($filter!="")
  519. {
  520. $filter="where ".$filter;
  521.  
  522.  
  523. }
  524. if($filter!=""){
  525. $filter2=" and ";
  526. }
  527. else{
  528.  
  529. $filter2=" where ";
  530.  
  531. }
  532.  
  533.  
  534. $filter2=$filter2."id<".$last_ad_id;
  535. }
  536.  
  537. $query="select * from ad $filter $filter2 order by id desc limit 2";
  538.  
  539. $result=mysqli_query($link,$query);
  540. $num=mysqli_num_rows($result);
  541.  
  542. $ad_list=array();
  543. while($row=mysqli_fetch_assoc($result))
  544. {
  545. $ad_list[]=$row;
  546.  
  547.  
  548. }
  549. print_r($ad_list);
  550.  
  551.  
  552.  
  553. exit();
  554. }
  555.  
  556.  
  557. //get_my_ad_list
  558. if ($command=="get_my_ad_list")
  559. {
  560.  
  561.  
  562. $user_id=$post_data['user_id'];
  563.  
  564.  
  565. $query="select * from ad where user_id=$user_id";
  566.  
  567. $result=mysqli_query($link,$query);
  568. $num=mysqli_num_rows($result);
  569.  
  570. $ad_list=array();
  571. while($row=mysqli_fetch_assoc($result))
  572. {
  573. $ad_list[]=$row;
  574.  
  575.  
  576. }
  577. echo json_encode($ad_list);
  578.  
  579.  
  580.  
  581.  
  582.  
  583. exit();
  584. }
  585.  
  586.  
  587.  
  588.  
  589.  
  590. //new_ad
  591. if ($command=="new_ad")
  592. {
  593.  
  594. $date=new DateTime();
  595.  
  596. $date->setTimezone(new DateTimeZone("Asia/Tehran"));
  597.  
  598. $current_timestamp = $date->getTimestamp();
  599.  
  600. //upload_voice
  601. if (isset($_POST['voice']))
  602. {
  603.  
  604.  
  605. $random_name = time().'_'.$_FILES["file_request"]['name'];
  606. move_uploaded_file($_FILES['file_request']['tmp_name'], 'voices/'. $random_name);
  607. $voice_name = $random_name;
  608.  
  609.  
  610. }else $voice_name=null;
  611.  
  612.  
  613.  
  614. //insert_etelaate_edittext_va_spinner_va_category
  615. echo $query="INSERT INTO `ad` (`user_id`, `title`, `description`, `category`, `voice`, `date`) VALUES (".$post_data['user_id'].", '".$post_data['title']."', '".$post_data['description']."', '".$post_data['category']."', '$voice_name' , CURRENT_TIMESTAMP);";
  616.  
  617. mysqli_query($link,$query);
  618.  
  619.  
  620.  
  621. echo "<myapp>"."ok"."</myapp>";
  622.  
  623.  
  624.  
  625.  
  626.  
  627. exit();
  628. }
  629.  
  630.  
  631. //upload_voice
  632.  
  633.  
  634.  
  635. //send_activation_key
  636. if ($command=="send_activation_key")
  637. {
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644. $query="delete from user where mobile='".$post_data['mobile']."'";
  645.  
  646. mysqli_query($link,$query);
  647.  
  648.  
  649.  
  650.  
  651. $activation_key=rand(1000,9999);
  652.  
  653.  
  654. $query="insert into user(mobile)values('".$post_data['mobile'].")";
  655.  
  656. mysqli_query($link,$query);
  657.  
  658.  
  659.  
  660.  
  661.  
  662. echo "<sheypoor>"."ok"."</sheypoor>";
  663.  
  664.  
  665. exit();
  666. }
  667.  
  668.  
  669.  
  670.  
  671. //apply_activation_key
  672. if ($command=="apply_activation_key")
  673. {
  674.  
  675. $mobile=$post_data['mobile'];
  676.  
  677.  
  678.  
  679.  
  680. $query="select * from user where mobile='$mobile'";
  681.  
  682. $result=mysqli_query($link,$query);
  683.  
  684. $num=mysqli_num_rows($result);
  685.  
  686.  
  687. if ($num!=0)
  688. {//activation ok
  689.  
  690.  
  691.  
  692.  
  693. mysqli_query($link,$query);
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702. $query="select * from user where mobile='$mobile'";
  703.  
  704. $result=mysqli_query($link,$query);
  705.  
  706. $num=mysqli_num_rows($result);
  707.  
  708.  
  709. $agent=$_SERVER['HTTP_USER_AGENT'];
  710.  
  711.  
  712. if ($num==0)
  713. {
  714.  
  715.  
  716. $query="insert into user(mobile)values('$mobile')";
  717.  
  718. mysqli_query($link,$query);
  719.  
  720.  
  721. $user_id=mysqli_insert_id($link);
  722.  
  723. }
  724. else
  725. {
  726.  
  727.  
  728. $row=mysqli_fetch_assoc($ersult);
  729.  
  730. $user_id=$row['id'];
  731.  
  732.  
  733. $query="update user set mobile='$mobile'";
  734.  
  735. mysqli_query($link,$query);
  736.  
  737.  
  738.  
  739.  
  740.  
  741. }
  742.  
  743.  
  744.  
  745. echo "<sheypoor>".$user_id."</sheypoor>";
  746.  
  747.  
  748.  
  749. }else
  750. {
  751.  
  752. echo "<sheypoor>"."error"."</sheypoor>";
  753.  
  754. }
  755.  
  756.  
  757.  
  758.  
  759.  
  760.  
  761.  
  762.  
  763. exit();
  764. }
  765.  
  766.  
  767.  
  768.  
  769.  
  770. ?>
Add Comment
Please, Sign In to add comment