Justin The
- Forum posts: 1
Apr 9, 2012, 10:47:03 AM via Website
Apr 9, 2012 10:47:03 AM via Website
Hi all,
I'm trying to create an app to upload a file to a web server.
here is a snippet of my code:
I then call that method from the doInBackground method.
my webserver is written in C# (will post if needed).
everytime it try to run the app (in emulator), i got a "Address family not supported by protocol" message. I've added INTERNET and WRITE_EXTERNAL_STORAGE permission in my manifest.
Anybody can give me guidance please ?
thank you in advance
Justin The
I'm trying to create an app to upload a file to a web server.
here is a snippet of my code:
1synchronized void doUpload()
2 {
3 String lineEnd = "\r\n";
4 String twoHyphens = "--";
5 String boundary = "*****";
6 Log.d(TAG,"lv1");
7
8 try
9 {
10 Log.d(TAG,"doUpload");
11 publishProgress("Uploading...");
12 HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
13 conn.setDoInput(true);
14 conn.setDoOutput(true);
15 conn.setUseCaches(false);
16 conn.setRequestMethod("POST");
17 conn.setRequestProperty("Connection","Keep-Alive");
18 conn.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);
19 DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
20 dos.writeBytes(twoHyphens + boundary + lineEnd);
21 dos.writeBytes("Content-Disposition:form-data; name=\"uploadedfile\";filename=\"" + fileName + "\"" + lineEnd);
22 dos.writeBytes(lineEnd);
23
24 Log.d(TAG,"LvA");
25 Log.d(TAG,twoHyphens + boundary + lineEnd + ";Content-Disposition:form-data; name=\"uploadedfile\";filename=\"" + fileName + "\"" + lineEnd);
26
27 int bytesAvailable = fileInputStream.available();
28 int maxBufferSize = 1024;
29 int bufferSize = Math.min(bytesAvailable, maxBufferSize);
30
31 byte[] buffer = new byte[bufferSize];
32
33 int bytesRead = fileInputStream.read(buffer,0, bufferSize);
34 Log.d(TAG,"LvB");
35 while(bytesRead > 0)
36 {
37 dos.write(buffer, 0, bufferSize);
38 bytesAvailable = fileInputStream.available();
39 bufferSize = Math.min(bytesAvailable, maxBufferSize);
40 bytesRead = fileInputStream.read(buffer, 0, bufferSize);
41 }
42
43 dos.writeBytes(lineEnd);
44 dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
45
46 fileInputStream.close();
47 dos.flush();
48
49 InputStream is = conn.getInputStream();
50 int ch;
51 Log.d(TAG,"LvC");
52 StringBuffer buff = new StringBuffer();
53 while((ch=is.read()) != -1)
54 {
55 buff.append((char)ch);
56 }
57 publishProgress(buff.toString());
58 dos.close();
59 Log.d(TAG,"lv2");
60 }
61
62 catch(Exception e)
63 {
64 publishProgress(e.toString());
65 }
66 }
2 {
3 String lineEnd = "\r\n";
4 String twoHyphens = "--";
5 String boundary = "*****";
6 Log.d(TAG,"lv1");
7
8 try
9 {
10 Log.d(TAG,"doUpload");
11 publishProgress("Uploading...");
12 HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
13 conn.setDoInput(true);
14 conn.setDoOutput(true);
15 conn.setUseCaches(false);
16 conn.setRequestMethod("POST");
17 conn.setRequestProperty("Connection","Keep-Alive");
18 conn.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);
19 DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
20 dos.writeBytes(twoHyphens + boundary + lineEnd);
21 dos.writeBytes("Content-Disposition:form-data; name=\"uploadedfile\";filename=\"" + fileName + "\"" + lineEnd);
22 dos.writeBytes(lineEnd);
23
24 Log.d(TAG,"LvA");
25 Log.d(TAG,twoHyphens + boundary + lineEnd + ";Content-Disposition:form-data; name=\"uploadedfile\";filename=\"" + fileName + "\"" + lineEnd);
26
27 int bytesAvailable = fileInputStream.available();
28 int maxBufferSize = 1024;
29 int bufferSize = Math.min(bytesAvailable, maxBufferSize);
30
31 byte[] buffer = new byte[bufferSize];
32
33 int bytesRead = fileInputStream.read(buffer,0, bufferSize);
34 Log.d(TAG,"LvB");
35 while(bytesRead > 0)
36 {
37 dos.write(buffer, 0, bufferSize);
38 bytesAvailable = fileInputStream.available();
39 bufferSize = Math.min(bytesAvailable, maxBufferSize);
40 bytesRead = fileInputStream.read(buffer, 0, bufferSize);
41 }
42
43 dos.writeBytes(lineEnd);
44 dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
45
46 fileInputStream.close();
47 dos.flush();
48
49 InputStream is = conn.getInputStream();
50 int ch;
51 Log.d(TAG,"LvC");
52 StringBuffer buff = new StringBuffer();
53 while((ch=is.read()) != -1)
54 {
55 buff.append((char)ch);
56 }
57 publishProgress(buff.toString());
58 dos.close();
59 Log.d(TAG,"lv2");
60 }
61
62 catch(Exception e)
63 {
64 publishProgress(e.toString());
65 }
66 }
I then call that method from the doInBackground method.
my webserver is written in C# (will post if needed).
everytime it try to run the app (in emulator), i got a "Address family not supported by protocol" message. I've added INTERNET and WRITE_EXTERNAL_STORAGE permission in my manifest.
Anybody can give me guidance please ?
thank you in advance
Justin The
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.