2016-02-18 60 views
-1

我想在android.I發送圖像到REST API我已經創建了一個android應用程序,它只是簡單地向API請求調用並返回響應,但知道我必須將Image發佈到REST API。Post Image到Android的REST API

這是我的代碼。

private class PostImage extends AsyncTask<String, String, String> { 
     @Override 
     protected String doInBackground(String... data) { 
     // Create a new HttpClient and Post Header 
     HttpClient httpclient = new DefaultHttpClient(); 
     URL url = new URL("http://localhost:8080/JAXRS-FileUpload/rest/files/upload"); 
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

     } 
    } 

我該如何提出發佈圖像到REST API的請求。這裏是其餘的api代碼。

@Path("/files") 
public class JerseyFileUpload { 
@POST 
    @Path("/upload") 
    @Consumes(MediaType.) 
    @Produces(MediaType.APPLICATION_JSON) 
    public ImageUrl responseMsg(){ 
    //do something 
    } 

哪種介質類型應該用來消耗圖像。

回答

0

我個人使用loopj進行客戶端 - 服務器通信,並且可以使用它中的文件發送圖像。添加下面的依賴於你的應用程序gradle這個使用循環J

compile 'com.loopj.android:android-async-http:1.4.9' 

然後發送圖像

public void sendAllData() { 
    String Url = "your url here"; 
    //If any auth is needed 
    String username = "username"; 
    String password = "password"; 

    Cursor cursor = mContext.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, Media.DATE_ADDED, null, "date_added ASC"); 
    if (cursor != null && cursor.moveToLast()) { 
     Uri fileURI = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA))); 
     fileSrc = fileURI.toString(); 
     cursor.close(); 
    } 

    // Bitmap compressedImage = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray())); 
    AsyncHttpClient client = new AsyncHttpClient(); 
    client.setBasicAuth(username, password); 
    RequestParams params = new RequestParams(); 
    try { 
     params.put("pic", storeImage()); 
    } catch (FileNotFoundException e) { 
     Log.d("MyApp", "File not found!!!" + fileSrc); 
    } 
    client.post(Url, params, new JsonHttpResponseHandler() { 
     @Override 
     public void onSuccess(int statusCode, Header[] headers, JSONObject responseBody) { 
      //Do what's needed 
     } 

     @Override 
     public void onFailure(int statusCode, Header[] headers, String errorResponse, Throwable error) { 
      //Print error 
     } 
    }); 

} 

private File storeImage() { 
    String filename = "anyName"; 
    String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); 
    OutputStream outStream = null; 

    File file = new File(extStorageDirectory, filename + ".jpg"); 
    try { 
     outStream = new FileOutputStream(file); 
     bookImage.compress(CompressFormat.JPEG, 80, outStream); 
     outStream.flush(); 
     outStream.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return file; 
} 
0

您實現服務並在它發佈圖片,請檢查我的樣本here

檢查postMultiPart方法。

1
MultipartEntity entity = new MultipartEntity(); 
FileBody userPhoto = new FileBody(new File(path)); 
entity.addPart("file", userPhoto); 

String url = UPDATE_PROFILE_URL; 
HttpParams params1 = new BasicHttpParams(); 
HttpConnectionParams.setStaleCheckingEnabled(params1, false); 

int timeOut = 2 * 60 * 1000; 

HttpConnectionParams.setConnectionTimeout(params1, timeOut); 
HttpConnectionParams.setSoTimeout(params1, timeOut); 
DefaultHttpClient httpClient = new DefaultHttpClient(params1); 
HttpPost httpPost = new HttpPost(url); 
httpPost.setEntity(entity); 
HttpResponse response = null; 
JSONObject jsResp = null; 
     try { 
      response = httpClient.execute(httpPost); 
      HttpEntity resEntity = response.getEntity(); 

      String sServerResponse = EntityUtils.toString(resEntity); 
      Log.i("abc", "FROM: POST:" + sServerResponse); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
+0

如何接收服務器上的形象說的是哪的MediaType我應該在REST API使用? –

+0

在這種情況下,Web服務上的媒體類型將爲MediaType.MULTIPART_FORM_DATA – Ishaan

0

修改成: 的multipart/form-data的

+0

我想通過imageview顯示許多圖像。例如,我有一個10個索引的數組,並且在循環中我想顯示10個圖像,我該怎麼做。 –

+0

你有數組中的URL? –