I was trying to upload an image on a server here is my code that i used to upload file from my android fragment:
class Uploader{
String selectedImagePath;
String fileSize;
String date;
Uploader(String selectedImagePath, String fileSize, String date){
this.selectedImagePath=selectedImagePath;
this.fileSize=fileSize;
this.date=date;
}
void upload(){
SharedPreferences pref = SpaceFragment.this.getActivity().getSharedPreferences("currentuser", 0);
String name = pref.getString("username", null);
String param2 = name;
String fileNameArray[] = selectedImagePath.split("/");
String fileName = fileNameArray[fileNameArray.length-1];
SendHttpRequestTask t = new SendHttpRequestTask();
Toast.makeText(SpaceFragment.this.getActivity(), "CURRENTLY UPLOADING "+selectedImage+","+param2, Toast.LENGTH_LONG).show();
String[] params = new String[]{url, selectedImagePath, param2};
t.execute(params);
}
private class SendHttpRequestTask extends AsyncTask<String, Void, String> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(SpaceFragment.this.getActivity());
progressDialog.setMessage("Uploading File. Please wait");
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
String url = params[0];
String param1 = params[1];
String param2 = params[2];
String fileNameArray[] = param1.split("/");
String fileName = fileNameArray[fileNameArray.length-1];
try {
Bitmap b = BitmapFactory.decodeFile(param1);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
b.compress(CompressFormat.PNG, 0, baos);
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
MultipartEntity multiPart = new MultipartEntity();
multiPart.addPart("param1", new StringBody(param1));
multiPart.addPart("param2", new StringBody(param2));
//Toast.makeText(getApplicationContext(),"SIZE IS "+ baos.toByteArray().length, Toast.LENGTH_LONG).show();
multiPart.addPart("file", new ByteArrayBody(baos.toByteArray(), fileName));
post.setEntity(multiPart);
client.execute(post);
}
catch(Throwable t) {
t.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String data) {
progressDialog.dismiss();
//SpaceFragment.this.cursor.requery();
//SpaceFragment.this.cursorAdapter.notifyDataSetChanged();
mySQLiteAdapter = new DriveDatabaseAdapter(SpaceFragment.this.getActivity().getApplicationContext());
mySQLiteAdapter.openToWrite();
SharedPreferences pref = SpaceFragment.this.getActivity().getSharedPreferences("currentuser", 0);
String name = pref.getString("username", null);
String fileNameArray[] = selectedImagePath.split("/");
String fileName = fileNameArray[fileNameArray.length-1];
mySQLiteAdapter.insertEntry(name,"IMAGE",fileName,fileSize,date);
}
}
};
But the problem is its giving java SocketException.
Here is StackTrace :
11-20 12:41:25.944: W/System.err(14489): java.net.SocketException: sendto failed: EPIPE (Broken pipe) 11-20 12:41:25.949: W/System.err(14489): at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:499) 11-20 12:41:25.950: W/System.err(14489): at libcore.io.IoBridge.sendto(IoBridge.java:468) 11-20 12:41:25.950: W/System.err(14489): at java.net.PlainSocketImpl.write(PlainSocketImpl.java:508) 11-20 12:41:25.951: W/System.err(14489): at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46) 11-20 12:41:25.951: W/System.err(14489): at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:270) 11-20 12:41:25.952: W/System.err(14489): at org.apache.http.impl.io.AbstractSessionOutputBuffer.write(AbstractSessionOutputBuffer.java:109) 11-20 12:41:25.952: W/System.err(14489): at org.apache.http.impl.io.ContentLengthOutputStream.write(ContentLengthOutputStream.java:113) 11-20 12:41:25.953: W/System.err(14489): at org.apache.http.impl.io.ContentLengthOutputStream.write(ContentLengthOutputStream.java:119) 11-20 12:41:25.953: W/System.err(14489): at org.apache.http.entity.mime.content.ByteArrayBody.writeTo(ByteArrayBody.java:83) 11-20 12:41:25.954: W/System.err(14489): at org.apache.http.entity.mime.HttpMultipart.doWriteTo(HttpMultipart.java:206) 11-20 12:41:25.954: W/System.err(14489): at org.apache.http.entity.mime.HttpMultipart.writeTo(HttpMultipart.java:224) 11-20 12:41:25.955: W/System.err(14489): at org.apache.http.entity.mime.MultipartEntity.writeTo(MultipartEntity.java:183) 11-20 12:41:25.955: W/System.err(14489): at org.apache.http.impl.entity.EntitySerializer.serialize(EntitySerializer.java:97) 11-20 12:41:25.956: W/System.err(14489): at org.apache.http.impl.AbstractHttpClientConnection.sendRequestEntity(AbstractHttpClientConnection.java:162) 11-20 12:41:25.956: W/System.err(14489): at org.apache.http.impl.conn.AbstractClientConnAdapter.sendRequestEntity(AbstractClientConnAdapter.java:272) 11-20 12:41:25.957: W/System.err(14489): at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:237) 11-20 12:41:25.957: W/System.err(14489): at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:119) 11-20 12:41:25.964: W/System.err(14489): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:442)
When I run same code in Activity it runs fine.
I got this on searching: Android : Socket - java.net.SocketException: sendto failed: EPIPE (Broken pipe)
But it's also not helping me out
Here is my complete code in case it can help in finding the problem: http://pastebin.com/s50R4daP