2016-09-29 3 views
-4

Json 키를 JsonObject로 보내려고합니다. 따라서 발리를 사용하여 보내는 방법을 알려주십시오. json = { "product": "magie"}발리에서 게시물 요청시 이러한 유형의 데이터를 보내는 방법은 무엇입니까?

Volley에서 데이터를 보내는 방법에 대해 해당 유형의 데이터와 함께 히트 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에 넣어 데이터처럼 다음 키 JSON과 해시 맵으로 된 JSONObject를 전달합니다.

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"); 
관련 문제