2016-08-08 18 views
1

私はVolleyに取り組んでいますが、このフレームワークにはかなり新しいです。ボレーを使用してJSONデータを投稿する方法がわかりません。サンプルJSONがあります。このJSONデータを投稿する方法を教えてください。アンドロイドを使ってJSONをボレーに投稿するには?

JSONObject jsonObject = new JSONObject(); 
jsonObject.put("email", "[email protected]"); 
jsonObject.put("password", "abcd123"); 
jsonObject.put("device", "jdghfdhgdhi"); 
jsonObject.put("latitude", 1.2456); 
jsonObject.put("longitude", 1.3466); 

私のJSONを通過し、このJSONをボレーで投稿する方法を教えてください。

+1

可能な重複し、このコードを試してみてくださいhttp://stackoverflow.com/questions/23220695/send-post-request-with-json-data-using-volley) – Sanoop

+0

http://stackoverflow.com/ques 23220695/send-post-request with json-data-using-volleyその回答を見てください –

+0

http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-ボレー/ボレーについて –

答えて

0

これを試してみてください。

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

          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 
        }, 
        new Response.ErrorListener() { 
         @Override 
         public void onErrorResponse(VolleyError error) { 
          Toast.makeText(NoolReaderDashboard.this, error.toString(), Toast.LENGTH_LONG).show(); 
         } 
        }) { 
       @Override 
       protected Map<String, String> getParams() { 
        Map<String, String> params = new HashMap<String, String>(); 
      params .put("email", "[email protected]"); 
     params .put("password", "abcd123"); 
     params .put("device", "jdghfdhgdhi"); 
     params .put("latitude", 1.2456); 
     params .put("longitude", 1.3466); 
        return params; 
       } 
      }; 
      RequestQueue requestQueue = Volley.newRequestQueue(this); 
      requestQueue.add(stringRequest); 
+0

ありがとうございますが、params.put( "latitude"、1.2456); params .put( "経度"、1.3466);それはエラーを表示しています "間違った引数が二重に見つかりました" –

+1

params .put( "latitude"、String.valueOf(1.2456));とparams .put( "経度"、String.valueOf(1.3466)); – onkar

+0

は文字列に変換し、それを(1.2456 + ""またはString.valueOf(1.2456));)@ AnshumanPattnaik – YUVRAJ

0

ます。private void POSTDATA(){

String url = "http://www.example.com";        //url where you want to post data 
    StringRequest postRequest = new StringRequest(Request.Method.POST, url, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        try { 
         Log.i("response is","==>"+response); 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
        Log.d("Response-------", "------>" + response); 
       } 
      }, 
      new Response.ErrorListener() { 
       public void onErrorResponse(VolleyError error) { 
        Log.e("Responce error==","===>"+error); 
        error.printStackTrace(); 
       } 
      } 
    ) { 

     protected Map<String, String> getParams() { 
      Map<String, String> params = new HashMap<>(); 
      // the POST parameters: 
      params.put("email", "[email protected]"); 
      params.put("password", "abcd123"); 
      params.put("latitude", 1.2456); 
      params.put("longitude", 1.3466); 
      Log.d("Value is ----------", ">" + params); 
      return params; 
     } 
    }; 
    MyApplication .getInstance().addToRequestQueue(postRequest); 
} 
0

は([ボレーを使用してJSONデータをPOSTリクエストを送信]の

JSONObject jsonObject = new JSONObject(); 
       try { 
     jsonObject.put("email", "[email protected]"); 
     jsonObject.put("password", "abcd123"); 
     jsonObject.put("device", "jdghfdhgdhi"); 
     jsonObject.put("latitude", 1.2456); 
     jsonObject.put("longitude", 1.3466); 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 


     final JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 


// here parse your response json data 

        try { 
         String status = response.getString("XSTS"); 


        } catch (JSONException e) { 
         Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show(); 
         e.printStackTrace(); 
        } 

       } 
      }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        // Handle your error types accordingly.For Timeout & No connection error, you can show 'retry' button. 
        // For AuthFailure, you can re login with user credentials. 
        // In this case you can check how client is forming the api and debug accordingly. 
        // For ServerError 5xx, you can do retry or handle accordingly. 
        if (error instanceof NetworkError) { 
         Toast.makeText(LoginScreenActivity.this, "No internet connection", Toast.LENGTH_SHORT).show(); 
        } else if (error instanceof ServerError) { 
         Toast.makeText(LoginScreenActivity.this, "Our server is busy please try again later", Toast.LENGTH_SHORT).show(); 
        } else if (error instanceof AuthFailureError) { 
         Toast.makeText(LoginScreenActivity.this, "AuthFailure Error please try again later", Toast.LENGTH_SHORT).show(); 
        } else if (error instanceof ParseError) { 
         Toast.makeText(LoginScreenActivity.this, "Parse Error please try again later", Toast.LENGTH_SHORT).show(); 
        } else if (error instanceof NoConnectionError) { 
         Toast.makeText(LoginScreenActivity.this, "No connection", Toast.LENGTH_SHORT).show(); 
        } else if (error instanceof TimeoutError) { 
         Toast.makeText(LoginScreenActivity.this, "Server time out please try again later", Toast.LENGTH_SHORT).show(); 
        } 
        error.printStackTrace(); 
        progressDialog.dismiss(); 
       } 
      }) { 
       @Override 
       public Map<String, String> getHeaders() throws AuthFailureError { 
        HashMap<String, String> headers = new HashMap<String, String>(); 
        headers.put("Content-Type", "application/json"); 

        return headers; 
       } 
      }; 
      Volley.newRequestQueue(LoginScreenActivity.this).add(postRequest); 
関連する問題