2015-11-25 106 views
1

這是我工作正常的HttpURLConnection請求。 我送JSON字符串與請求 現在我已經轉換申請凌空如何將HttpURL連接請求轉換爲Volly請求?

String jsonStr = {"email":"[email protected]","full_name":"vghjj", 
        "locations":[],"mobile_no":"XXXXXXXXX", 
        "super_sectors": [],"unique_device_id":"XXXXXXX","utm_params":"No Utm Params"}" 


try { 
     HttpURLConnection urlConnection = getHttpConnection(jsonStr, url, oauth, REQUEST_TYPE.POST.getType()); 

     if (urlConnection != null) { 
      if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { 
       result = convertInputStreamToString(urlConnection.getInputStream()); 
      } 
     } 
    } catch (Exception e) { 
     return null; 
    } 

private static HttpURLConnection getHttpConnection(String jsonStr , String strUrl, Oauth oauth, String type) { 
    URL url = null; 
    HttpURLConnection urlConnection = null; 

    try { 
     url = new URL(strUrl); 
     /* */ 
     if (GlobalVariables.DEVELOPING) 
      Log.v(TAG, url.toString()); 

     urlConnection = (HttpURLConnection) url.openConnection(); 
     urlConnection.setRequestMethod(type); 
     urlConnection.setConnectTimeout(GlobalVariables.applyInternetConnectionTimeOut); 
     urlConnection.setReadTimeout(GlobalVariables.applyInternetSocketTimeOut); 
     urlConnection.setRequestProperty("Accept", "application/json"); 
     urlConnection.setRequestProperty("Content-type", "application/json"); 
     urlConnection.setRequestProperty("Authorization", oauth.getToken_type() + " " + oauth.getAccess_token()); 

     OutputStream os = urlConnection.getOutputStream(); 
     os.write(jsonStr.getBytes()); 
     os.flush(); 
     os.close(); 

     return urlConnection; 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

我寫了這個代碼凌空抽射爲同一請求

StringRequest= new StringRequest(Request.Method.POST, url, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 

        // Successfull Stuff 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 

        Log.d(TAG, "onErrorResponse: " + error.toString()); 

       } 
      } 
    ) { 

     @Override 
     protected Map<String, String> getParams() { 
      Map<String, String> params = new HashMap<String, String>(); 
      params.put("allValue", jsonStr); 
      return params; 
     } 

     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      Map<String, String> params = new HashMap<String, String>(); 
      params.put("Content-Type", "application/json"); 
      params.put("Accept", "application/json"); 
      params.put("Authorization", oauth.getToken_type() + " " + oauth.getAccess_token()); 
      return params; 
     } 
    }; 

    /*Generate Queue to line up apply request*/ 
    RequestQueue requestQueue = VollySingleton.getsInstance().getmRequestQueue(); 
    requestQueue.add(stringRequest); 

所以我的具體問題是在哪裏呢那jsonStr進入Volley請求 帶字符串請求它給出400錯誤(錯誤請求) With JSonObject Request它給出500錯誤

在HTTP URL連接每一件事情是工作的罰款,並jsonStr在

OutputStream os = urlConnection.getOutputStream(); 
      os.write(jsonStr.getBytes()); 
     os.flush(); 
     os.close(); 
+0

如果你正試圖從一些服務器獲取'JSON'數據,使用'JsonObjectRequest'。接下來,您正在使用的代碼不適用於硬編碼的JSON字符串。你的代碼試圖通過你提到的URL從服務器上獲取JSON數據。看看這個教程,你會知道'volley'是如何工作的。 (http://code.tutsplus.com/tutorials/an-introduction-to-volley--cms-23800) –

+0

我讀過這個文檔,但從來沒有發現任何關於jsonStr的請求,在HttpURLConnection中它們是OutputStream的Option,其中是選項或類似的選項,我問了有關問題。 –

+0

'public void onResponse(String response){...}'''response'與您的'jsonStr'類似。 –

回答

0

獲得通過我這是怎麼解決這是在事件HttpClient的請求習慣StringEntity問題 創建的對象

StringEntity entity = new StringEntity(JsonStr); 

然後Overrride方法

@Override 
     public String getBodyContentType() { 
      return entity.getContentType().getValue(); 
     } 

     @Override 
     public byte[] getBody() { 
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
      try { 
       entity.writeTo(outputStream); 
      } catch (IOException e) { 
       VolleyLog.e("IOException @ " + getClass().getSimpleName()); 
      } 
      return outputStream.toByteArray(); 
     } 

所以完整的答案將是

StringRequest= new StringRequest(Request.Method.POST, url, 
     new Response.Listener<String>() { 
      @Override 
      public void onResponse(String response) { 

       // Successfull Stuff 
      } 
     }, 
     new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 

       Log.d(TAG, "onErrorResponse: " + error.toString()); 

      } 
     } 
) { 

    @Override 
     public String getBodyContentType() { 
      return entity.getContentType().getValue(); 
     } 

     @Override 
     public byte[] getBody() { 
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
      try { 
       entity.writeTo(outputStream); 
      } catch (IOException e) { 
       VolleyLog.e("IOException @ " + getClass().getSimpleName()); 
      } 
      return outputStream.toByteArray(); 
     } 


    @Override 
    public Map<String, String> getHeaders() throws AuthFailureError { 
     Map<String, String> params = new HashMap<String, String>(); 
     params.put("Content-Type", "application/json"); 
     params.put("Accept", "application/json"); 
     params.put("Authorization", oauth.getToken_type() + " " + oauth.getAccess_token()); 
     return params; 
    } 
}; 

/*Generate Queue to line up apply request*/ 
RequestQueue requestQueue = VollySingleton.getsInstance().getmRequestQueue(); 
requestQueue.add(stringRequest); 
相關問題