2016-04-20 18 views
1

私はRadioButtonと結婚していますが、結婚していないと私は何をしなければならないのですか?ユーザーチェック結婚ラジオボタンjsonsを使用してサーバーに「結婚」の値を送信しなければなりません。ここでAndroidボタンでサーバーにラジオボタンの値を送信する方法は?

は、JSONコードのフィールドです: - コード以下

jsonObject.put("maritalstatus", ""); 

答えて

1

試してみてください。

String status = jsonObject.getString("maritalstatus"); 
if(status.equals("married")){ 
    radioGroup.check(radioGroup.getChildAt(0).getId()); 
}else{ 
    radioGroup.check(radioGroup.getChildAt(1).getId()); 
} 
+0

私は応答が結婚しているか、未婚の可能性があることを確認した後、私は武道の状態になります。 – niraj

+0

私はurのコメントを取得していません。どのような反応が得られ、どのような応答が必要ですか? –

+0

私は結婚したときにサーバーから結婚しました。結婚したラジオを設定しなければなりません。結婚していない場合は、未婚のRdaioButtonをチェックしてください。 – niraj

0

まず、各RadioButtonの要素に対して:

RadioGroup radioGroup = (RadioGroup)findViewById(R.id.yourid); 
String selectedText = ((RadioButton)findViewById(radioGroup.getCheckedRadioButtonId())).getText().toString(); 

jsonObject.put("maritalstatus", selectedText); 

確認のラジオボタンを設定するには後で検索するためのタグを設定する必要があります。

marriedRadioButton.setTag("married"); 
unmarriedRadioButton.setTag("unmarried") 


RadioGroup rdGroup = (RadioGroup) findViewById(R.id.rdbGp1); 
rdGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 

@Override 
public void onCheckedChanged(RadioGroup group, int checkedId) { 
    RadioButton selectedRadioButton = (RadioButton) group.findViewById(checkedId); 
    String text = radioButton.getTag(); 
    JSONObject jsonObject = new JSONObject(); 
    jsonObject.put("maritalstatus", text); 
    // Now you need to send the json to the server through an AsyncTask 
    SendJsonTask sendJsonTask = new SendJsonTask(); 
    sendJsonTask.execute(jsonObject.toString()); 
}}); 

private class SendJsonTask extends AsyncTask<String, Void, Void> { 
protected String doInBackground(String... params) { 
    URL url = new URL("http://domaintoreceive.com/pagetoreceive"); 

    HttpClient httpClient = new DefaultHttpClient(); 
    HttpPost httpPost = new HttpPost(url.toURI()); 

    httpPost.setEntity(new StringEntity(params[0], "UTF-8")); 

    // Set up the header types needed to properly transfer JSON 
    httpPost.setHeader("Content-Type", "application/json"); 
    httpPost.setHeader("Accept-Encoding", "application/json"); 
    httpPost.setHeader("Accept-Language", "en-US"); 

    // Execute POST 
    response = httpClient.execute(httpPost); 
}} 

必要なすべての例外を忘れることを忘れないでください。

よろしくお願いいたします。