如何 Post 排球中的 Spinner 值

How to Post Spinner Value in Volley

我正在使用 VolleyPOST json 对象到 Django Server.The 我需要的对象 post 的形式是

{
"name": "A",
"joined":true,
"description":"Info",
"status":"F",
}  

我的 Spinner 包含此值以获取用户的状态输入

<array name="status">
        <item>Free</item>
        <item>Busy</item>
    </array>  

如果选择的微调值是自由的,我需要 post 我的 Json Object.How 中的 F 状态来读取微调值,并根据选择的位置如何 post 这个 JSON 对象 ?

使用 OnItemSelectedListener 作为微调器,然后在代码行下方写下。

这样做:-

String item =spinner.getItemSelected.toString()

试试看

        JSONObject object= new JSONObject();
        try {
            object.put("name","A");
            object.put("joined",true);
            object.put("description","description");
            if(spinner.getItemSelected.toString().equals("Free")){
                object.put("status","F");
            }else {
                object.put("status","S");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

试试这个:

如果您使用 contains 方法,当字符串为 Fire、Forest 等时它可能为 false....word contains F

    if(spinner.getItemSelected.toString().equalsIgnoreCase("Free")) { 
        // set "status" F to json
    }
    else {
        // set "status" B to json
    }

从微调器中获取选定文本的代码,

String text = spinner.getSelectedItem().toString();  // This will give you "FREE"

从字符串中获取第一个字符的代码,

text = text.subString(0,1);  // This will return first character "F"
String text = String.valueOf(spinner.getSelectedItem().toString().charAt(0));

JSONObject object= new JSONObject();
    try {
        object.put("name","User Name");
        object.put("joined",true);
        object.put("description","User Description");
        object.put("status",text);
    } catch (JSONException e) {
        e.printStackTrace();
    }
String url = "http://....../kkk.php";

私人无效提交数据() {

    String item =spinner.getItemSelected.toString()

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

                    final String result=response.toString().trim();



                    Log.d("response", "result : "+result); //when response come i will log it
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error)
                {

                    error.printStackTrace();
                    error.getMessage(); // when error come i will log it
                }
            }
    )
    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String>  params = new HashMap<String, String>();

            params.put("item", item);


            return params;
        }
    };
    Vconnection.getnInstance(this).addRequestQue(stringRequest); // vConnection i claas which used to connect volley


}

Vconnection class 代码....

public class Vconnection {

private static Vconnection nInstance;

private RequestQueue RQ;

private Context CTX;

private Vconnection(Context context)
{
    CTX=context;
    RQ=getRequestQue();
}

public RequestQueue getRequestQue()
{
    if(RQ==null)
    {
        RQ= Volley.newRequestQueue(CTX.getApplicationContext());
    }
    return RQ;
}
public static synchronized Vconnection getnInstance(Context context)
{
    if(nInstance==null)
    {
        nInstance=new Vconnection(context);
    }
    return nInstance;
}
public <T> void addRequestQue(Request<T> request)
{
    RQ.add(request);
}

}

如果您使用 contains 方法,当字符串为 "Fire""Forest" 等时它可能为 false....

if(spinner.getItemSelected.toString().equalsIgnoreCase("Free")) { 
    // set "status" F to json
}
else
    // set "status" B to json