I am developing an android application in which i have to post some data to url.I did googling and got some samples.I tried them out.But i did not got the response as r开发者_运维百科equired.
Below is the link which i tried
http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient
Has anyone done this before.If yes can he help me
Thanks in advance Tushar
I create a class with all my information I need to send and serialize it to a xml file with simple xml. Then I send the entire XML. But you can send fields only too:
static String CRLF = "\r\n";
static String twoHyphens = "--";
static String boundary = "*****mgd*****";
private DataOutputStream dataStream = null;
private void postData(){
try{
URL connectURL = new URL("http://example.com/upload.php");
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("User-Agent", "test");
conn.setRequestProperty("Connection","Keep-Alive");
conn.setRequestProperty("Content-Type","multipart/form-data;boundary="+boundary);
conn.connect();
dataStream = new DataOutputStream(conn.getOutputStream());
//Send fields
writeFormField("name", "bla");
writeFormField("password", "bla");
//Send a file
File uploadFile = new File(SD_CARD_TEMP_DIR, "file.xml");
FileInputStream fileInputStream = new FileInputStream(uploadFile);
writeFileField("myFile", "somefilename.xml", "text/xml",fileInputStream);
// final closing boundary line
dataStream.writeBytes(twoHyphens + boundary +twoHyphens + CRLF);
fileInputStream.close();
dataStream.flush();
dataStream.close();
dataStream = null;
boolean response = getResponse(conn);
if(response)
{
finish();
}
}
catch (MalformedURLException mue) {
// TODO
}
catch (IOException ioe) {
// TODO
}
catch (Exception e) {
// TODO
}
}
private void writeFormField(String fieldName, String fieldValue) {
try {
dataStream.writeBytes(twoHyphens + boundary + CRLF);
dataStream.writeBytes("Content-Disposition: form-data;name=\"" + fieldName + "\"" + CRLF);
dataStream.writeBytes(CRLF);
dataStream.writeBytes(fieldValue);
dataStream.writeBytes(CRLF);
} catch (Exception e) {
System.out.println("writeFormField:got: " + e.getMessage());
}
}
private void writeFileField(
String fieldName,
String fieldValue,
String type,
FileInputStream fis)
{
try
{
// opening boundary line
dataStream.writeBytes(twoHyphens + boundary + CRLF);
dataStream.writeBytes("Content-Disposition: form-data; name=\""
+ fieldName
+ "\";filename=\""
+ fieldValue
+ "\""
+ CRLF);
dataStream.writeBytes("Content-Type: " + type + CRLF);
dataStream.writeBytes(CRLF);
// create a buffer of maximum size
int bytesAvailable = fis.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
// read file and write it into form...
int bytesRead = fis.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
dataStream.write(buffer, 0, bufferSize);
bytesAvailable = fis.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fis.read(buffer, 0, bufferSize);
}
// closing CRLF
dataStream.writeBytes(CRLF);
}
catch(Exception e)
{
Log.i(TAG, "writeFormField: got: " + e.getMessage());
}
}
private boolean getResponse(HttpURLConnection conn)
{
try {
// try doing this in one read
InputStream data = conn.getInputStream();
StringBuffer sb = new StringBuffer();
//Reader reader = new InputStreamReader(data, "UTF-8");
int c;
while ((c = data.read()) != -1) sb.append((char) c);
String document = sb.toString();
int responseCode = conn.getResponseCode();
return true;
}
catch(Exception e)
{
}
return false;
}
精彩评论