2016-09-29 5 views
-4

これは、JsonObjectをキーJsonに送信します。それで、ボレーを使って送る方法を教えてください。 json = {"product": "magie"}ボレーでのポストリクエストでこのタイプのデータを送信するにはどうすればいいですか?

このデータをVolleyで送信するには、そのタイプのデータでhit apiのasyncTaskコードを追加します。

enter code here 

protected void onPreExecute() { 
    if (progress) 
     GlobalAlerts.showProgressDialog(context); 
} 

@Override 
protected String doInBackground(String... params) { 
    String resp = null; 
    JSONObject jsonObject = new JSONObject(); 
    try { 
     jsonObject.put("product","magie"); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    String data = "json=" + jsonObject.toString(); 
    String url ="http://anc.php"; 
    resp = new JsonCall().executeHttpPostRequest(url, data); 
    return resp; 
} 

protected void onPostExecute(String resp) { 
    if (progress) 
     GlobalAlerts.hideProgressDialog(); 
    if (resp != null) { 
     callback.onTaskComplete(resp); 
    } else { 
     GlobalAlerts.singleAlert((Activity) context, context.getString(R.string.warning), "Error", false); 
    } 
} 
+0

はあなたのコードを投稿ここでコードを入力してください。 – vinoth12594

+0

これをチェックしてくださいhttps://www.simplifiedcoding.net/android-volley-post-request-tutorial/ここであなたが行ったいくつかのコードを投稿してください – Shailesh

+0

あなたは次のリンクから回答を見つけることができます: - [> http:///stackoverflow.com/questions/24873718/how-do-i-make-a-volley-jsonobject-request-with-a-custom-object-as-a-parameter][1] –

答えて

0

答えはjsonObjectにデータを入れてから、jsonObjectをキーjsonでhashmapに渡すようなものです。

String url = "http://anc.php"; 
    JSONObject jsonObject = new JSONObject(); 
    try { 
     jsonObject.put("product", "magie"); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    final HashMap<String,String> hashMap2 = new HashMap<>(); 
    hashMap2.put("json",jsonObject.toString()); 

    StringRequest strReq = new StringRequest(Request.Method.POST, 
      url, new Response.Listener<String>() { 

     @Override 
     public void onResponse(String response) { 
      Log.e("order_list", response); 
      GlobalAlerts.hideProgressDialog(); 
     } 
    }, new Response.ErrorListener() { 

     @Override 
     public void onErrorResponse(VolleyError error) { 
      Log.e("Error", "Error: " + error.getMessage()); 
      GlobalAlerts.hideProgressDialog(); 
     } 
    }) { 
     @Override 
     protected Map<String, String> getParams() { 
      /*Map<String,String> params = new HashMap<>(); 
      params.put("employee_id"," 1");*/ 
      return hashMap2; 
     } 
    }; 
    Application.getInstance().addToRequestQueue(strReq, "order_list"); 
関連する問題