2013-06-29 25 views
1

場景 - 在帖子正文中上傳二進制數據,處理包含JSON的響應主體。android volley發佈二進制主體

如何使用Volley進行以下操作?

curl -X POST -H "X-Application-Id: 3KxPB" -H "X-REST-API-Key: jkuI9" -H "Content-Type: audio/3gp" --data-binary '@test.3gp' https://host/1/files/audio 

IMO - 存在凌空處理二進制POST主體類型阿帕奇HttpClient的處理中的abstracthttpentity子類的一個缺口。如果攝像頭,麥克風或其他二進制輸出傳感器在手機上生成的緩衝二進制數據需要包裝並寫入POST主體的機制,請問如何排除這些數據?

我已經看過PoolingByteArrayOutputStream,並且想要做一些類似於填充緩衝區並獲取PBAOutStrm,從緩衝區寫入PBAOutStrm,然後將OutStrm轉換爲InputStream,然後將其包裝在POST請求的主體中像一個ByteArrayEntity。我無法看到如何在凌空中做到這一點。

+0

羅伯特,你有沒有得到一個答案?我試圖做同樣的事情(完成--data-binary參數),下面的答案沒有幫助。 – FractalBob

+0

沒有最終使用這種類型的POST的排球。其他客戶端 - http-libs可以完成這項工作。 –

回答

0

我能解決這個使用凌空GsonRequest:

公共類MainActivity擴展AppCompatActivity {

String url = "https://arcane-anchorage-34204.herokuapp.com/handleCode"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    JSONObject jsonBody = null; 
    try { 
     jsonBody = new JSONObject ("{\"code\":\"NZ4UBUB\"}"); 
    } catch (JSONException e) { 
     Toast.makeText(getApplicationContext(), "Error e = " + e, Toast.LENGTH_SHORT).show(); 
    } 

    Map<String, String> headers = new HashMap<String, String>(); 
    headers.put("Content-Type", "application/json"); 

    RequestQueue queue = Volley.newRequestQueue(this); 

    GsonRequest<Routine[]> gsonRequest = new GsonRequest<Routine[]>(Request.Method.POST, url, Routine[].class, headers, new Response.Listener<Routine[]>() { 
     @Override 
     public void onResponse(Routine[] routineData) { 
      TextView serverData = (TextView)findViewById(R.id.serverData); 

      String complete = ""; 
      String repeat = ""; 
      String hold = ""; 
      String perform = ""; 
      String txtData = ""; 

      for (int i = 0; i < routineData.length; i++) { 
       complete = (routineData[i].instructions.complete != null) ? "Complete: " + routineData[i].instructions.complete : ""; 
       repeat = (routineData[i].instructions.repeat != null) ? "Repeat: " + routineData[i].instructions.repeat : ""; 
       hold = (routineData[i].instructions.hold != null) ? "Hold: " + routineData[i].instructions.hold : ""; 
       perform = (routineData[i].instructions.perform != null) ? "Perform: " + routineData[i].instructions.perform : ""; 

       txtData += "DESCRIPTION: " + routineData[i].description[0] + ": " + routineData[i].description[1] + ", " + complete + ", " + repeat + ", " + hold + ", " + perform + " "; 
      } 
      serverData.setText("Response: " + txtData); 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError volleyError) { 
      TextView serverData = (TextView)findViewById(R.id.serverData); 

      serverData.setText("Response: " + volleyError.toString()); 

     } 
    }, jsonBody); 
    queue.add(gsonRequest); 
} 
public class GsonRequest<T> extends Request<T> { 
    private final Gson gson = new Gson(); 
    private final Class<T> clazz; 
    private final Map<String, String> headers; 
    private final Response.Listener<T> listener; 
    private JSONObject parameters = null; 

    /** 
    * Make a GET request and return a parsed object from JSON. 
    * 
    * @param url URL of the request to make 
    * @param clazz Relevant class object, for Gson's reflection 
    * @param headers Map of request headers 
    */ 
    public GsonRequest(int method, String url, Class<T> clazz, Map<String, String> headers, 
         Response.Listener<T> listener, Response.ErrorListener errorListener) { 
     super(method, url, errorListener); 
     this.clazz = clazz; 
     this.headers = headers; 
     this.listener = listener; 
    } 

    public GsonRequest(int method, String url, Class<T> clazz, Map<String, String> headers, 
         Response.Listener<T> listener, Response.ErrorListener errorListener, JSONObject parameters) { 
     this(method, url, clazz, headers, listener, errorListener); 
     this.parameters = parameters; 
    } 

    @Override 
    public Map<String, String> getHeaders() throws AuthFailureError { 
     return headers != null ? headers : super.getHeaders(); 
    } 

    @Override 
    public String getBodyContentType() { 
     return "application/json"; 
    } 

    @Override 
    public byte[] getBody() throws AuthFailureError { 
     try { 
      return parameters.toString().getBytes(getParamsEncoding()); 
     } catch (UnsupportedEncodingException e) { 
     } 
     return null; 
    } 

    @Override 
    protected void deliverResponse(T response) { 
     listener.onResponse(response); 
    } 

    @Override 
    protected Response<T> parseNetworkResponse(NetworkResponse response) { 
     try { 
      String json = new String(
        response.data, HttpHeaderParser.parseCharset(response.headers)); 
      Log.i("RESPONSE", json); 
      return Response.success(
        gson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response)); 
     } catch (UnsupportedEncodingException e) { 
      return Response.error(new ParseError(e)); 
     } catch (JsonSyntaxException e) { 
      return Response.error(new ParseError(e)); 
     } 
    } 
    } 
}