【问题标题】:How to Post Spinner Value in Volley如何在 Volley 中发布 Spinner 值
【发布时间】:2018-09-04 11:33:57
【问题描述】:

我正在使用VolleyPOST json对象到Django Server。我需要发布的对象是

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

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

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

如果选择的微调器值是免费的,我需要在我的 Json 对象中将状态发布为 F。如何读取微调器值并根据选择的位置如何发布此 JSON 对象?

【问题讨论】:

  • 那么,您只想将微调器的第一个字符发送到服务器?
  • String text = spinner.getSelectedItem().toString(); 将给出选定的文本。
  • 你必须实现 OnItemSelectedListener....
  • 问题似乎太宽泛了,标签太多了。考虑问一个精确的问题。
  • 是的,我只想发送第一个字符

标签: android json django-rest-framework android-volley android-spinner


【解决方案1】:

试试这个:

如果你使用 contains 方法,当字符串为 Fire、Forest 等时它可能为 false ......单词包含 F

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

【讨论】:

  • @Zaphod 他只有两个元素,其中只有一个元素包含 F .. 检查问题! :) 提建议
【解决方案2】:

试试看

        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();
        }

【讨论】:

    【解决方案3】:

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

    这样做:-

    String item =spinner.getItemSelected.toString()
    

    【讨论】:

      【解决方案4】:

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

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

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

      text = text.subString(0,1);  // This will return first character "F"
      

      【讨论】:

        【解决方案5】:
        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();
            }
        

        【讨论】:

          【解决方案6】:
          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 类代码....

          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);
          }
          

          }

          【讨论】:

            【解决方案7】:

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

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2019-07-09
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-04-28
              相关资源
              最近更新 更多