如何在Android中使用JSON将微调器值发送到服务器?

时间:2015-02-09 13:34:07

标签: android json string spinner

我是Android新手。我无法将微调器值发送到jsonobject。我收到一个错误:“选中无法解析为变量”。以下是我的代码。请告诉我我需要做什么。提前致谢。

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<String> list = new ArrayList<String>();
    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<String> dataAdapter = new ArrayAdapter<String>
    (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 & 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 <NameValuePair> nvps = new ArrayList <NameValuePair>();
                       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<?> 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<?> parent) {
    // TODO Auto-generated method stub

}

1 个答案:

答案 0 :(得分:1)

您收到

行中的错误

registration.put("grade", selected);

因为变量selected是在onItemSelected方法

中本地声明的

全局声明selected变量。

或者你可以使用

registration.put("grade", grade );

registration.put("grade", spinner.getSelectedItem().toString() );