2016-08-08 10 views
0

私はいくつかの応答を得るためにサーバーに送信する必要がある次の生データを持っています。ボレーを使用して生データを投稿する方法は?

{ 
"nodeId": null, 
"userId": null, 
"mobileNumber": "0000000", 
"emailId": "[email protected]", 
"userProfile": null, 
"region": null, 
"countryCode": "01", 
"password": "[email protected]", 
"places": [], 
"trustedNetwork": [], 
"profilePic": null, 
"fullName": null, 
"longitude": 0.0, 
"latitude": 0.0 } 

生データを送信する際に、認証ヘッダーパラメータも送信する必要があります。何を試しても、投稿できませんでした。ボレーの例外ケースを育て許可を作成中

私はここでまだ

RequestQueue requestQueue = Volley.newRequestQueue(getActivity()); 
    String URL = baseURL; 

    final String mRequestBody = s; 

    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      Log.i("VOLLEY", response); 
      new AlertDialog.Builder(getActivity()) 
        .setTitle("hmm") 
        .setMessage(""+response) 
        .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          dialog.dismiss(); 

         } 
        }) 
        .show(); 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
     Log.e("VOLLEY", error.getLocalizedMessage()); 
      new AlertDialog.Builder(getActivity()) 
        .setTitle("SORRY") 
        .setMessage(""+error.getLocalizedMessage()) 
        .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          dialog.dismiss(); 

         } 
        }) 
        .show(); 

     } 
    }) { 
     @Override 
     public String getBodyContentType() { 
      return "application/json; charset=utf-8"; 
     } 
     @Override 
     public byte[] getBody() throws AuthFailureError { 
      try { 
       return mRequestBody == null ? null : mRequestBody.getBytes("utf-8"); 
      } catch (UnsupportedEncodingException uee) { 
       VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", 
        mRequestBody, "utf-8"); 
      return null; 
      } 

     } 
     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      HashMap<String, String> headers = new HashMap<String, String>(); 
      String creds = String.format("%s:%s", "xxxxxx", "xxxxxxxxxxxxxx"); 
      String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT); 
      headers.put("Authorization", auth); 
      headers.put("Content-Type", "application/json; charset=utf-8"); 
      return headers; 
     } 


    }; 

    requestQueue.add(stringRequest); 
+0

'mRequestBody'。あなたは今それが何を含んでいるかを示しています。 's'? – greenapps

+0

'まだ動作していません.' ??あなたは何が起こったのか、何が起こらないのかをすばやく伝えるべきです。 – greenapps

+0

@greenapps mRequestBodyは文字列の形の生のjsonを含んでいます。私はこのコードを実行するたびに "ヌル"のボレー例外を受け取ります。 – mrnobody

答えて

0

働いていない、次のコードを使用している、問題が実際に発生していました。以来、私は使用していたパスワードは非常に長いBase64.defaultは正しく処理されていませんでした。 Base64.no_wrapに変更した後、問題は解決しました

関連する問題