我正在使用Volley
到POST
json对象到Django Server。我需要发布的对象是
{
"name": "A",
"joined":true,
"description":"Info",
"status":"F",
}
我的微调器包含此值以从用户
获取状态输入<array name="status">
<item>Free</item>
<item>Busy</item>
</array>
如果选择的微调器值是Free,我需要在我的Json Object中将状态发布为F.如何读取微调器值并根据所选位置如何发布此JSON
对象?
答案 0 :(得分:1)
试试这个
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();
}
答案 1 :(得分:1)
试试这个:
如果使用contains方法,当字符串为Fire,Forest等时,它可能为false ....单词包含F
if(spinner.getItemSelected.toString().equalsIgnoreCase("Free")) {
// set "status" F to json
}
else {
// set "status" B to json
}
答案 2 :(得分:0)
对微调器使用OnItemSelectedListener
,然后记下代码行。
这样做: -
String item =spinner.getItemSelected.toString()
答案 3 :(得分:0)
从微调器中获取所选文本的代码,
String text = spinner.getSelectedItem().toString(); // This will give you "FREE"
从字符串中获取第一个字符的代码,
text = text.subString(0,1); // This will return first character "F"
答案 4 :(得分:0)
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();
}
答案 5 :(得分:0)
String url = "http://....../kkk.php";
private void SubmitData() {
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);
}
}
答案 6 :(得分:0)
如果使用contains方法,当字符串为"Fire"
,"Forest"
等时,它可能会为false ....
if(spinner.getItemSelected.toString().equalsIgnoreCase("Free")) {
// set "status" F to json
}
else
// set "status" B to json