0

hi all i am trying to upload image file from sdcard to php server in android. i use the following code for uploading , it does not show exception but the image is not uploaded in server. i do not know what is the problem. please assist me. i attached my code and Logcat information. code:

public class UploadImage extends Activity {
InputStream is;
private int serverResponseCode;
private String serverResponseMessage;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String pathToOurFile = "//sdcard//chsevtoneta.png";
String urlServer = "http://xxxxxxxxxxxxxxxxxxxxxxxx/upload.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary =  "*****";

int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;

try
{
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );

URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();

// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);

// Enable POST method
connection.setRequestMethod("POST");

connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

outputStream = new DataOutputStream( connection.getOutputStream() );
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
outputStream.writeBytes(lineEnd);

bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];

// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);

while (bytesRead > 0)
{
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}

outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

// Responses from the server (code and message)

serverResponseCode = connection.getResponseCode();
serverResponseMessage = connection.getResponseMessage();
Log.e("response",""+serverResponseCode);
Log.e("serverResponseMessage",""+serverResponseMessage);

fileInputStream.close();
outputStream.flush();
outputStream.close();
}
catch (Exception ex)
{
//Exception handling
}
}
}

and Logcat information:

 05-10 11:04:59.762: ERROR/response(2203): 200
 05-10 11:04:59.762: ERROR/serverResponseMessage(2203): OK
4
  • 1
    Your server is returning a success message: what does your PHP code look like? Commented May 10, 2011 at 6:56
  • thanks but the server coding is not available for me. but for other the server returns correct response [ server return a url]. any method to get response? Commented May 10, 2011 at 7:29
  • Yeah, that will be hard to debug then: do you have any one else's working code? Commented May 10, 2011 at 7:30
  • sorry for delay, the problem is happen in android only but the server gives response correctly in iphone. that's why i couldn't attach' others' iphone coding. Commented May 10, 2011 at 11:48

2 Answers 2

2

The remote servers answers with a success (200) message. I think the error may be in the server side, but you don't provide any code for this part.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks but the server coding is not available for me. but for other the server returns correct response [ server return a url]. any method to get response?
1

Looking at your source it appears that you are building a multipart/form upload by hand: this can easily be error-prone, adding a line break or an extra space in the wrong place would be very difficult to spot by eye but could easily confuse the server (depending on the strictness of the HTTP parser in use).

I would recommend using Android's built-in HTTP components pieces to do it: an example is at http://indiwiz.com/2009/02/11/multi-part-content-upload-in-apache-http-components-http-client/.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.