title: How to send spinner value to server using JSON in Android?
body: <p>I am new on Android. I am not able to send spinner value to jsonobject. I am getting an error: "selected cannot be resolved to a variable". Following is my code. Please tell me what I need to do. Thanks in advance. </p>
  
  <pre><code>public class SignupActivity extends Activity implements OnItemSelectedListener {
  
  public Button buttonsign;
  public Spinner spinner;
  public EditText edit_message1, edit_message2, edit_message3, edit_message4, edit_message5, edit_message6, edit_message7, edit_message8;
  public String firstname, lastname, birthday, username, password, retypepassword, parentsemail;
  
  private static final String TAG = "SignupActivity.java";
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      setContentView(R.layout.activity_signup);
      getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
      addListenerOnButton();
      StrictMode.enableDefaults();
  
      spinner = (Spinner) findViewById(R.id.spinner);
      List&lt;String&gt; list = new ArrayList&lt;String&gt;();
      list.add("SELECT");
      list.add("Grade 1");
      list.add("Grade 2");
      list.add("Grade 3");
      list.add("Grade 4");
      list.add("Grade 5");
      list.add("Grade 6");
      list.add("Grade 7");
      list.add("Grade 8");
      list.add("Grade 9");
      list.add("Grade 10");
  
      ArrayAdapter&lt;String&gt; dataAdapter = new ArrayAdapter&lt;String&gt;
      (this, android.R.layout.simple_spinner_item,list);
  
      dataAdapter.setDropDownViewResource
      (android.R.layout.simple_spinner_dropdown_item);
  
      spinner.setAdapter(dataAdapter);
      spinner.setOnItemSelectedListener(this);
  }
  
  private void addListenerOnButton() {
      // TODO Auto-generated method stub
      buttonsign = (Button) findViewById(R.id.buttonsign);
      edit_message1=(EditText) findViewById(R.id.edit_message1);
      edit_message2=(EditText) findViewById(R.id.edit_message2);
      edit_message3=(EditText) findViewById(R.id.edit_message3);
      //edit_message4=(EditText) findViewById(R.id.spinner);
      edit_message5=(EditText) findViewById(R.id.edit_message5);
      edit_message6=(EditText) findViewById(R.id.edit_message6);
      edit_message7=(EditText) findViewById(R.id.edit_message7);
      edit_message8=(EditText) findViewById(R.id.edit_message8);
      spinner = (Spinner) findViewById(R.id.spinner);
  
  
      buttonsign.setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View view) {
  
              InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
              inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
  
  
              String firstname = edit_message1.getText().toString();
              String lastname = edit_message2.getText().toString();
              String birthday = edit_message3.getText().toString();
              String grade = spinner.getSelectedItem().toString();
              String username = edit_message5.getText().toString();
              String password = edit_message6.getText().toString();
              String retypepassword = edit_message7.getText().toString();
              String parentsemail = edit_message8.getText().toString();
  
  
              if(firstname.equals("")) {
  
                  Toast.makeText(SignupActivity.this,"Enter First Name", Toast.LENGTH_LONG).show();
              }
              else if(lastname.equals("")) {
  
                  Toast.makeText(SignupActivity.this,"Enter Last Name", Toast.LENGTH_LONG).show();
              }
              else if(birthday.equals("")) {
  
                  Toast.makeText(SignupActivity.this,"Enter Your Birthday", Toast.LENGTH_LONG).show();
              }
              /* else if(grade.equals("SELECT")) {
  
                  Toast.makeText(SignupActivity.this,"Enter Your Grade", Toast.LENGTH_LONG).show();
              }
              */
              else if(username.equals("")) {
  
                  Toast.makeText(SignupActivity.this,"Enter Username", Toast.LENGTH_LONG).show();
              }
              else if(password.equals("")) {
  
                  Toast.makeText(SignupActivity.this,"Enter Password", Toast.LENGTH_LONG).show();
              }
              else if(retypepassword.equals("")) {
  
                  Toast.makeText(SignupActivity.this,"Enter Re-Type Password", Toast.LENGTH_LONG).show();
              }
              else if(!retypepassword.equals(password)) {
  
                  Toast.makeText(SignupActivity.this,"Password Not Matching", Toast.LENGTH_LONG).show();
              }
              else if(username.equals(password)) {
  
                  Toast.makeText(SignupActivity.this,"Username &amp; Password should be different", Toast.LENGTH_LONG).show();
              }
              else if(parentsemail.equals("")) {
  
                  Toast.makeText(SignupActivity.this,"Enter Parent's Email", Toast.LENGTH_LONG).show();
              }
              else
              {       
  
                 try {
  
                     HttpClient client = new DefaultHttpClient();
                     String url = "http: ...";           
                     HttpPost request = new HttpPost(url);
                     Log.v(TAG, "postURL: " + request); 
  
  
                         JSONObject registration = new JSONObject();
                         registration.put("firstname", firstname);
                         registration.put("lastname", lastname);
                         registration.put("birthday", birthday);
                         registration.put("grade", selected);
                         registration.put("username", username);
                         registration.put("password", password);
                         registration.put("parentsemail", parentsemail);
  
                         JSONArray jsonArray = new JSONArray();
                         jsonArray.put(registration);
  
                         List &lt;NameValuePair&gt; nvps = new ArrayList &lt;NameValuePair&gt;();
                         nvps.add(new BasicNameValuePair("studentregistration", registration.toString()));
                         request.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
  
                         request.setHeader(HTTP.CONTENT_TYPE,"application/x-www-form-urlencoded;charset=UTF-8");
                         HttpResponse response = client.execute(request);
                         HttpEntity entity = response.getEntity();
                         if (entity != null) {
                             String responseStr = EntityUtils.toString(entity).trim();
                             Log.v(TAG, "Response: " +  responseStr);
                             Log.i("TAG",""+response.getStatusLine().getStatusCode());
                             Toast.makeText(SignupActivity.this,  responseStr, Toast.LENGTH_LONG).show();                    
                         }
                     Toast.makeText(SignupActivity.this, "studentregistration: " +registration,Toast.LENGTH_LONG).show();
                 } catch (ClientProtocolException e) {
                     e.printStackTrace();
                 } catch (IOException e) {
                     e.printStackTrace();
                 } catch (Throwable t) {
                     Toast.makeText(SignupActivity.this, "Request failed: " + t.toString(),
                             Toast.LENGTH_LONG).show();
                 }
  
  
                 }
              }
          });
          }
  
  
  @Override
  public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position,
          long id) {
      // TODO Auto-generated method stub
      String selected = parent.getItemAtPosition(position).toString();
      ((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE);
      ((TextView) parent.getChildAt(0)).setTextSize(24);
  
      if(selected.equals("SELECT")) {
  
          Toast.makeText(parent.getContext(),"Enter Your Grade", Toast.LENGTH_LONG).show();
      }
  }
  
  
  @Override
  public void onNothingSelected(AdapterView&lt;?&gt; parent) {
      // TODO Auto-generated method stub
  
  }
  </code></pre>