java - string.format throws exception in http post request and response -
i have method placing format specifiers (like %s) instead of placing hard code values, can find code bellow;
string username = "nicole";     string password = "nicole";      httpclient client = new defaulthttpclient();      httppost post = new httppost("url");      string input=string.format("<soap:envelope       xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:inn=\"http:innovation/\">      <soap:header/><soap:body><inn:getcategoriesbyvendorid><!--optional:-->      <username>%s</username><!--optional:--><password>%s</password></inn:getcategoriesbyvendorid>      </soap:body></soap:envelope>",username,password);          //stringentity input = new stringentity("<soap:envelope       xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:inn=\"http:innovation/\">      <soap:header/><soap:body><inn:getcategoriesbyvendorid><!--optional:-->      <username>nicole</username><!--optional:--><password>nicole</password>      </inn:getcategoriesbyvendorid></soap:body></soap:envelope>");            post.setentity(input);          httpresponse response = client.execute(post);          bufferedreader rd = new bufferedreader(new       inputstreamreader(response.getentity().getcontent()));          string line = "";          while ((line = rd.readline()) != null) {          system.out.println(line);   with code above, entity throws error. me solve issue?
there nothing wrong syntax of string formatting.
it showing error in eclipse e.,change type of input http entity(post. set entity(input);).
the error because httppost.setentity expects parameter of type httpentity while specifying type of string it.
try setting org.apache.http.entity.stringentity instead:
httpentity entity = new stringentity(input); //your formatted string  post.setentity(entity);      
Comments
Post a Comment