android - cannot get the response -
fileinputstream fileinputstream = new fileinputstream(sourcefile); url url = new url(uploadserveruri); // open http connection url conn = (httpurlconnection) url.openconnection(); conn.setdoinput(true); // allow inputs conn.setdooutput(true); // allow outputs conn.setusecaches(false); // don't use cached copy conn.setrequestmethod("post"); conn.setrequestproperty("connection", "keep-alive"); conn.setrequestproperty("enctype", "multipart/form-data"); conn.setrequestproperty("content-type", "multipart/form-data;boundary=" + boundary); conn.setrequestproperty("uploaded_file", filename); dos = new dataoutputstream(conn.getoutputstream()); dos.writebytes(twohyphens + boundary + lineend); dos.writebytes("content-disposition: form-data; name=\"uploaded_file\";filename=\"" + filename + "\"" + lineend); dos.writebytes(lineend); // create buffer of maximum size bytesavailable = fileinputstream.available(); buffersize = math.min(bytesavailable, maxbuffersize); buffer = new byte[buffersize]; // read file , write form... bytesread = fileinputstream.read(buffer, 0, buffersize); while (bytesread > 0) { dos.write(buffer, 0, buffersize); bytesavailable = fileinputstream.available(); buffersize = math.min(bytesavailable, maxbuffersize); bytesread = fileinputstream.read(buffer, 0, buffersize); } // send multipart form data necesssary after file data... dos.writebytes(lineend); dos.writebytes(twohyphens + boundary + twohyphens + lineend); // responses server (code , message) serverresponsecode = conn.getresponsecode(); **inputstream = conn.getinputstream(); bufferedreader rd = new bufferedreader(new inputstreamreader(is)); string line; stringbuffer response = new stringbuffer(); while((line = rd.readline()) != null) { response.append(line); response.append('\r'); } rd.close();** // string serverresponsemessage = conn.getresponsemessage(); log.i("uploadfile", "http response : " + line + ": " + serverresponsecode); if(serverresponsecode == 200){ runonuithread(new runnable() { public void run() { string msg = "file upload completed.\n\n see uploaded file here : \n\n" +" http://www.androidexample.com/media/uploads/" +uploadfilename; messagetext.settext(msg); toast.maketext(uploadtoserver.this, "file upload complete.", toast.length_short).show(); } }); } //close streams // fileinputstream.close(); dos.flush(); dos.close(); } catch (malformedurlexception ex) { dialog.dismiss(); ex.printstacktrace(); runonuithread(new runnable() { public void run() { messagetext.settext("malformedurlexception exception : check script url."); toast.maketext(uploadtoserver.this, "malformedurlexception", toast.length_short).show(); } }); log.e("upload file server", "error: " + ex.getmessage(), ex); } catch (exception e) { dialog.dismiss(); e.printstacktrace(); runonuithread(new runnable() { public void run() { messagetext.settext("got exception : see logcat "); toast.maketext(uploadtoserver.this, "got exception : see logcat ", toast.length_short).show(); } }); log.e("upload file server exception", "exception : " + e.getmessage(), e); } dialog.dismiss(); return serverresponsecode; } // end else block }
i used above code upload image onto server works fine.but need response coming server after image saved on sever.but null value.i used php echo statement write response .please me solove matter.thank you
Comments
Post a Comment