2017-06-24 17 views
2

私はJavaで新しいです。ポストjsonデータをWebサーバーに送信したい My Volleyの投稿を以下に示します。ここでAndroid Volley jsonのデータをサーバーに転送

public void postData(String url,JSONObject data,final VolleyCallback mResultCallback){ 
    RequestQueue requstQueue = Volley.newRequestQueue(mContext); 

    JsonObjectRequest jsonobj = new JsonObjectRequest(Request.Method.POST, url,null, 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 
        if(mResultCallback != null){ 
         mResultCallback.notifySuccess(response); 
        } 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        if(mResultCallback != null){ 
         mResultCallback.notifyError(error); 
        } 
       } 
      } 
    ){ 
     //here I want to post data to sever 
    }; 
    requstQueue.add(jsonobj); 

} 

は私MainActivityコード

JSONObject data = null; 
      try { 
       String datas = "{'email': email,'password': password}"; 
       data = new JSONObject(datas); 
      }catch (JSONException e){ 
       e.printStackTrace(); 
      } 
String url = "http://example.com"; 

である私は私のPostDataのメソッドにJSONデータを投稿したいです。 このjsonデータをサーバーに投稿するにはどうすればよいですか?

+0

'新しいJsonObjectRequest(Request.Method.POST、url、null、' JSONObject paramsの場合は3番目のパラメータ)。 nullをあなたのjsondataに置き換えてください –

答えて

1
public void postData(String url,Hashmap data,final VolleyCallback mResultCallback){ 
     RequestQueue requstQueue = Volley.newRequestQueue(mContext); 

     JsonObjectRequest jsonobj = new JsonObjectRequest(Request.Method.POST, url,new JSONObject(data), 
       new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject response) { 
         if(mResultCallback != null){ 
          mResultCallback.notifySuccess(response); 
         } 
        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         if(mResultCallback != null){ 
          mResultCallback.notifyError(error); 
         } 
        } 
       } 
     ){ 
      //here I want to post data to sever 
     }; 
     requstQueue.add(jsonobj); 

    } 

今、あなたのmainActiviyクラスから

Hashmap data = new HashMap(); 
    data.put("email","email"); 
    data.put("password","password");  
    String url = "http://example.com"; 

//now you can just call the method, I have rectified your string to hashmap, 
postData(url,data,new mResultCallb.....    //rest of your code 
0
public void postData(String url,final JSONObject data,final VolleyCallback mResultCallback){ 
    RequestQueue requstQueue = Volley.newRequestQueue(mContext); 

    JsonObjectRequest jsonobj = new JsonObjectRequest(Request.Method.POST, url,null, 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 
        if(mResultCallback != null){ 
         mResultCallback.notifySuccess(response); 
        } 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        if(mResultCallback != null){ 
         mResultCallback.notifyError(error); 
        } 
       } 
      } 
    ){ 
     @Override 
      protected Map<String, String> getParams() { 
       Map<String, String> params = new HashMap<String, String>(); 
       if(data != null){ 
        Iterator<String> keysItr = data.keys(); 
        while(keysItr.hasNext()) { 
          String key = keysItr.next(); 
          Object value = data.get(key); 

          if(value instanceof JSONArray) { 
           value = toList((JSONArray) value); 
          } 

          else if(value instanceof JSONObject) { 
           value = toMap((JSONObject) value); 
          } 
          params.put(key, value); 
         } 
       } 
       return params; 
      } 
    }; 
    requstQueue.add(jsonobj); 

これは私のためのコード

これはあなたのために働くかもしれない希望...

ハッピーCoddingを働いている

+0

これは要件を解決しません(JSON OBJでnullを渡します) – jagapathi

+0

私のAndroid Studionはdata.keys()、toList((JSONArray)value)を解決しませんでした。とtoMap((JSONObject)の値);方法 – alien

1
public void postData(String url,JSONObject data,final VolleyCallback mResultCallback){ 
RequestQueue requstQueue = Volley.newRequestQueue(mContext); 

JsonObjectRequest jsonobj = new JsonObjectRequest(Request.Method.POST, url,data, 
     new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 
       if(mResultCallback != null){ 
        mResultCallback.notifySuccess(response); 
       } 
      } 
     }, 
     new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       if(mResultCallback != null){ 
        mResultCallback.notifyError(error); 
       } 
      } 
     } 
); 
requstQueue.add(jsonobj); 

} 
関連する問題