2013-06-05 1 views
-2

내가 구현 한 REST 서비스에서 REST 서비스로 데이터를 전송합니다이미지를 업로드하기 위해 C#으로 자바

[OperationContract] 
[WebInvoke(Method = "PUT", UriTemplate = "add/{idAlbum}/{name}/image")] 
void Add(string idAlbum, string name, Stream image); 

나는 C#을 클라이언트와 함께 사용 성공 : 그래서 지금

byte[] image = lireFichier(@"C:\Users\user\Pictures\asap2.jpeg"); 
WebClient client = new WebClient(); 
client.Headers.Add("Content-Type", "image/jpeg"); 
var results = client.UploadData("http://localhost:1767/ImageService.svc/add/1/REST/image", "PUT", image); 

I 이런 (작동하지 않는) 자바 클라이언트 (안드로이드)와 함께 사용하려는 :

HttpURLConnection conn = (HttpURLConnection) new URL("http://localhost:1767/ImageService.svc/add/1/RESTjava/image").openConnection(); 

conn.setRequestMethod("PUT"); 

conn.setDoOutput(true); 
conn.connect(); 

OutputStream out = conn.getOutputStream(); 

Bitmap img = ((BitmapDrawable)getResources().getDrawable(R.drawable.entourage)).getBitmap(); 
ByteArrayOutputStream bao = new ByteArrayOutputStream(); 
img.compress(Bitmap.CompressFormat.JPEG, 90, bao); 
byte[] data = bao.toByteArray(); 
out.write(data); 

나는 어떤 오류가 발생하지 않지만 이것은 없습니다. 예외 없음.

06-05 14:11:57.736: I/ASAP PICS(746): onPreExecute 
06-05 14:11:57.745: I/ASAP PICS(746): doInBackground 
06-05 14:11:57.816: I/System.out(746): 405 
06-05 14:44:26.245: D/dalvikvm(971): GC freed 238 objects/332400 bytes in 34ms 
06-05 14:11:57.905: I/ASAP PICS(746): onPostExecute 
+0

예외? 리턴 코드? – Fildor

+0

예외 없음 .06-05 14 : 11 : 57.736 : I/ASAP PICS (746) : onPreExecute 06-05 14 : 11 : 57.745 : I/ASAP PICS (746) : doInBackground 06-05 14 : 11 : 57.816 : I/System.out (746) : 405 06-05 14 : 11 : 57.905 : I/ASAP PICS (746) : onPostExecute – Nahani

답변

0

나는 해결책을 찾았습니다!

[OperationContract] 
[WebInvoke(Method = "POST", UriTemplate = "add/{idAlbum}/{name}")] 
void Add(string idAlbum, string name, Stream image); 

을 마지막으로 자바 클라이언트 :

먼저 나는 POST 방법에 REST 서비스를 넣어

HttpPost post = new HttpPost("http://localhost:1767/ImageService.svc/add/1/RESTjava"); 

Bitmap img = ((BitmapDrawable)getResources().getDrawable(R.drawable.entourage)).getBitmap(); 
ByteArrayOutputStream bao = new ByteArrayOutputStream(); 
img.compress(Bitmap.CompressFormat.JPEG, 90, bao); 
byte[] data = bao.toByteArray(); 

ByteArrayEntity bimg = new ByteArrayEntity(data); 
post.setEntity(bimg); 
new DefaultHttpClient().execute(post); 
0

당신은 당신의 OutputStreamflush을 수행해야합니다 out.flush();. 이것이 답입니다.

+0

끝에 out.flush()를 추가했는데, work ... – Nahani

+0

@ user2455427 또한 추가 'out.close();' – Andremoniy

+0

작동하지 않지만 logcat에 메시지가 하나 더 있습니다. 06-05 14 : 13 : 15.415 : D/dalvikvm (809) : GC 해제 된 1128 개 개체/72032 바이트에서 44 초 – Nahani

0

I/System.out에 (746) : 405

HTTP 405 =이 방법 할 수 없습니다.

인증을 추가해야 할 것 같습니까?

+0

브라우저를 사용하여 URL을 방문 할 때이 메시지가 표시되므로 정상적인 것 같습니다. 그러나 C# 클라이언트에서 작동합니다. – Nahani

+1

그러면 C# 클라이언트가 다르게하고 있는지 확인해야 할 것입니다. 나는 인증과 관련된 일종의 내기가있을 것입니다. – Fildor

관련 문제