Upload file from android to server using OkHttp and PHP -
i make android uploader pand php script. can not upload csv
file android local folder.
php sclipt can work alone.
error undefined index:upfile in /example/html/php/ uploadfile.php online 9
do not choice file
public class mainactivity extends activity { textview outputtext; button senddata; edittext edtuser, edtpass; final string url = "http://example/php/uploadfile.php"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); outputtext = (textview) findviewbyid(r.id.textview1); outputtext.setmovementmethod(new scrollingmovementmethod()); senddata = (button) findviewbyid(r.id.button1); edtuser = (edittext) findviewbyid(r.id.edittext1); edtpass = (edittext) findviewbyid(r.id.edittext2); string text = "tue oct 06 18:56:24 jst 2015,tue oct 06 18:57:12 jst 2015,35.4708118,139.6251623\n"; try { outputstream out = openfileoutput("gps_log2.csv", mode_append); printwriter writer = new printwriter(new outputstreamwriter(out, "utf-8")); writer.append(text); writer.close(); } catch (ioexception e) { e.printstacktrace(); } senddata.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { okhttphandler handler = new okhttphandler(); string result = null; try { result = handler.execute(url).get(); } catch (interruptedexception e) { e.printstacktrace(); } catch (executionexception e) { e.printstacktrace(); } outputtext.append(result + "\n"); } }); } } public class okhttphandler extends asynctask<string, void, string> { public static final mediatype media_type_csv = mediatype.parse("text/csv; charset=utf-8"); okhttpclient client = new okhttpclient(); public okhttphandler(){ } @override protected string doinbackground(string........params){ string filename = "gps_log2.csv"; log.d("doinbackground", "start"); request request = new request.builder() .url("http://160.16.84.183/php/uploadfile.php") .post(requestbody.create(media_type_csv,filename)) .build(); try{ response response = client.newcall(request).execute(); if(!response.issuccessful()) throw new ioexception("unexpected code " + response.tostring()); return response.body().string(); }catch(exception e){ e.printstacktrace(); log.d("response ",e.getmessage()); } return null; } if (is_uploaded_file($_files["upfile"]["tmp_name"])) { if (move_uploaded_file($_files["upfile"]["tmp_name"], "/var/www/html/php/" . $_files["upfile"]["name"])) { chmod("/var/www/html/php/" . $_files["upfile"]["name"], 0644); echo $_files["upfile"]["name"] . "file uploaded"; } else { echo "cannot file upload"; } } else { echo "do not choice file"; }
aine , trying write file @ /var/www/html/php/. not best practice write files location . move loaction of script /home , change path etc/apache/siteenabled . more details http://askubuntu.com/questions/379412/how-to-redirect-apache-var-www-to-the-home-directory-and-access-all-the-directo php code must follows public static function processrequest() { case 'post': if(move_uploaded_file($_files["uploaded_file"]["tmp_name"] , "images/" . $_files["uploaded_file"]["name"] )){ echo "move_uploaded_file success "; ...................................... } ...................................... protected function executepost ($ch) { $tmpfile = $_files['image1']['tmp_name']; $filename = basename($_files['image1']['name']); $data = array( 'uploaded_file' => '@' . $tmpfile . ';filename='.$filename, ); curl_setopt($ch, curlopt_post, 1); curl_setopt($ch, curlopt_postfields, $data); //no need httpheaders $this->doexecute($ch); }
Comments
Post a Comment