Android post file and text -


i have 2 methods @ moment 1 post file, , post text, below

post data...

public void postdata() {       // create new httpclient , post header        arraylist<namevaluepair> namevaluepairs = new arraylist<namevaluepair>();     edittext et = (edittext) findviewbyid(r.id.entry);     string enteredname = et.gettext().tostring();     gender();     category();     namevaluepairs.add(new basicnamevaluepair("name",enteredname));     namevaluepairs.add(new basicnamevaluepair("gender",radio));     namevaluepairs.add(new basicnamevaluepair("cat",radio2));      //http post     try{             httpclient httpclient = new defaulthttpclient();             httppost httppost = new httppost("http://10.0.2.2:90/upload.php");             httppost.setentity(new urlencodedformentity(namevaluepairs));             httpresponse response = httpclient.execute(httppost);      } catch (clientprotocolexception e) {           // todo auto-generated catch block           } catch (ioexception e) {           // todo auto-generated catch block       }                   } 

post file...

public void postfile(){     file file = new file(filedir2);     try {              system.out.println(filedir2);              httpclient client = new defaulthttpclient();                string posturl = "http://10.0.2.2:90/mobileupload.php";                               httppost post = new httppost(posturl);             filebody bin = new filebody(file);          multipartentity reqentity = new multipartentity(httpmultipartmode.browser_compatible);            reqentity.addpart("image", bin);          post.setentity(reqentity);            httpresponse response = client.execute(post);            httpentity resentity = response.getentity();            if (resentity != null) {                        log.i("response",entityutils.tostring(resentity));              }     } catch (exception e) {         e.printstacktrace();     } } 

i have made php file combines both mobileupload.php , upload.php, wondering if there way 1 method , 1 post?

help appreciated

thanks

james

you can use this:

file file = new file("filetosend.txt"); httpclient client = new httpclient();  string url = "http://www.yourdomain.com/destination.php"; postmethod postmethod = new postmethod(url);  part[] parts = {new filepart(file.getname(), file)}; postmethod.setparameter("name", "value"); // set parameters instead in separate call  postmethod.setrequestentity( new multipartrequestentity(parts, postmethod.getparams()));  int status = client.executemethod(postmethod); 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -