2017-04-19 13 views
-1

UR1からスピナーを挿入しようとしていますが、GETメソッドを使用してスピナーに要素を取得できません... POSTメソッドは登録をうまくいきます...しかし、スピナー上にデータを取得...これは私のコードJSONObjectRequestボレーを使用してスピナーを挿入する方法

public class SuppRegActivity extends AppCompatActivity implements RegistrationView,View.OnClickListener { 

EditText company_name,code,Add1,Add2,Add3,Add4,Tel_no,mob_no,email,contact_person,tin_no,password; 
Spinner state; 
Button save; 
ArrayList<String> mystates; 
ArrayList<JSONObject> err; 

String Get_company_name,Get_code,Get_Add1,Get_Add2,Get_Add3,Get_Add4,Get_Telno, 
Get_mobno,Get_email,Get_contactp,Get_tinno,Get_password,Get_state; 

RegistrationPresenter registrationPresenter; 
ProgressDialog progressDialog; 
View v; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_supp_reg); 

    company_name = (EditText) findViewById(R.id.suppname); 
    code = (EditText) findViewById(R.id.suppcode); 
    Add1 = (EditText) findViewById(R.id.Add1); 
    Add2 = (EditText) findViewById(R.id.Add2); 
    Add3 = (EditText) findViewById(R.id.Add3); 
    Add4 = (EditText) findViewById(R.id.Add4); 
    Tel_no = (EditText) findViewById(R.id.suppPhone); 
    mob_no = (EditText) findViewById(R.id.SuppMob); 
    email = (EditText) findViewById(R.id.suppEmail); 
    contact_person= (EditText) findViewById(R.id.SuppPerson); 
    tin_no = (EditText) findViewById(R.id.SuppTin); 
    password = (EditText) findViewById(R.id.suppPass); 
    state = (Spinner) findViewById(R.id.suppstate); 
    save = (Button) findViewById(R.id.Saveregister); 
    save.setOnClickListener(this); 
    //arr = new ArrayList<JSONObject>(); 

    registrationPresenter = new RegistrationPresenterImpl(this, SuppRegActivity.this); 


} 

@Override 
public void onClick(View view) { 

    Get_company_name = company_name.getText().toString(); 
    Get_Add1 = Add1.getText().toString(); 
    Get_Add2 = Add2.getText().toString(); 
    Get_Add3 = Add3.getText().toString(); 
    Get_Add4 = Add4.getText().toString(); 
    Get_state = state.getSelectedItem().toString(); 
    Get_Telno = Tel_no.getText().toString(); 
    Get_mobno = mob_no.getText().toString(); 
    Get_email = email.getText().toString(); 
    Get_contactp = contact_person.getText().toString(); 
    Get_tinno = tin_no.getText().toString(); 
    Get_code = code.getText().toString(); 
    Get_password = password.getText().toString(); 



    if(!Get_company_name.equals("")){ 
     if(!Get_Add1.equals("")){ 
      if(!Get_Add2.equals("")){ 
       if(!Get_Add3.equals("")){ 
        if(!Get_Add4.equals("")){ 
         if(!Get_state.equals("")){ 
          if(!Get_Telno.equals("")){ 
           if(Get_Telno.length() ==10){ 
            if(!Get_mobno.equals("")){ 
             if(Get_mobno.length() == 10){ 
              String emailPattern = "[a-zA-Z0-9._-][email protected][a-z]+\\.+[a-z]+"; 
              if(Get_email.matches(emailPattern)){ 
               if(!Get_contactp.equals("")){ 
                if(!Get_tinno.equals("")){ 
                 if(!Get_code.equals("")){ 
                  if(!password.equals("")){ 
                   doRegisteration(Get_company_name,Get_code,Get_Add1,Get_Add2,Get_Add3,Get_Add4,Get_state,Get_Telno,Get_mobno,Get_email,Get_contactp, 
                     Get_tinno,Get_password); 
                  }else{ 
                   showFailedAlertBox("Password in Required"); 
                  } 
                 }else{ 
                  showFailedAlertBox("user name is required"); 
                 } 
                }else{ 
                 showFailedAlertBox("enter Tin No."); 
                } 
               }else{ 
                showFailedAlertBox("enter contact person"); 
               } 
              }else{ 
               showFailedAlertBox("invalid email"); 
              } 
             }else{ 
              showFailedAlertBox("enter 10 digit phone no"); 
             } 
            }else{ 
             showFailedAlertBox("enter mobile no"); 
            } 
           }else{ 
            showFailedAlertBox("enter 10 digit phone no."); 
           } 
          }else{ 
           showFailedAlertBox("enter mobile no"); 
          } 
         }else{ 
          showFailedAlertBox("State not selected"); 
         } 
        }else{ 
         showFailedAlertBox("Address line 4 required"); 
        } 
       }else{ 
        showFailedAlertBox("Address line 3 required"); 
       } 
      }else{ 
       showFailedAlertBox("Address line 2 required"); 
      } 
     }else{ 
      showFailedAlertBox("Address line 1 required"); 
     } 
    }else{ 
     showFailedAlertBox("Company name is required"); 
    } 


} 

private void showSuccessAlertBox(final String v) { 
    new AlertDialog.Builder(this).setMessage(v) 
      .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        if (v.equals("Registration Done")) { 
         /* // String uNameForIntent = company_name.getText().toString().trim(); 
         Intent intent = new Intent(SuppRegActivity.this, MainActivity.class); 
         // intent.putExtra("uname", uNameForIntent); 
         startActivity(intent);*/ 

        } 

        else{ 
         return; 
        } 
       } 
      }).show(); 
} 

@TargetApi(Build.VERSION_CODES.KITKAT) 
public void doRegisteration(String companyname, String code, String Add1, String Add2, String Add3, String Add4, String State, String tel_no, 
          String mobileno, String email, String contactp, String tinno, String password){ 

    /*JSONObject stat = new JSONObject(); 
    try{ 
     JSONObject a = stat.getJSONObject("getAllState"); 
     for(int i=0;i<a.length();i++) { 
      JSONObject c=(JSONObject) a.get(i); 


     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    }*/ 




    JSONObject register= new JSONObject(); 
    try { 
     register.put("FullName",Get_company_name); 
     register.put("Alias",Get_code); 
     register.put("Add1", Get_Add1); 
     register.put("Add2", Get_Add2); 
     register.put("Add3", Get_Add3); 
     register.put("Add4", Get_Add4); 
     register.put("State",Get_state); 
     register.put("TelNo",Get_Telno); 
     register.put("mobile", Get_mobno); 
     register.put("email", Get_email); 
     register.put("TinNo",Get_tinno); 
     register.put("ContactPerson", Get_contactp); 
     register.put("Password", Get_password); 

       JSONObject s = register.getJSONObject("http://.............................../getAllState"); 
        err = new ArrayList<JSONObject>(); 
        err.add(s); 

     ArrayAdapter<JSONObject> adapter = new ArrayAdapter<JSONObject>(this,android.R.layout.simple_spinner_item,err); 
     state.setAdapter(adapter); 

    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 

    } 

    String REGISTER_URL =" http://........................../AddAccount"; 
    //String STATE_URL = "http://.........................../getAllState"; 

    showProgress(); 

    /*JsonObjectRequest statelist = new JsonObjectRequest(Request.Method.GET, STATE_URL, stat, 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 
        state.setAdapter(new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item,arr)); 
       } 
      }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 

     } 
    });*/ 

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, REGISTER_URL,register, 
      new Response.Listener<JSONObject>() { 

       @Override 
       public void onResponse(JSONObject response) { 
        JSONObject j = null; 



        String result = null; 
        try { 
         result = response.getString("Result"); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

        if (result.equals("Saved")) { 
         showSuccessAlertBox("Registration Done"); 
         //volleyForProfile(sessionManager.getUserName()); 

        } 
        else 
         showFailedAlertBox("Failed Process!!!Try Again"); 


        hidePregress(); 

       } 
      }, 
      new Response.ErrorListener() { 

       @Override 
       public void onErrorResponse(VolleyError error) { 
        hidePregress(); 
       } 
      } 
    ); 
    request.setRetryPolicy(new DefaultRetryPolicy(
      30000, 
      DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
      DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 
    VolleyApplication.getInstance().getRequestQueue().add(request); 
} 

private void showFailedAlertBox(String v) { 
    new AlertDialog.Builder(this).setMessage(v) 
      .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.cancel(); 
       } 
      }).show(); 
} 

@Override 
public void showProgress() { 
    progressDialog = ProgressDialog.show(SuppRegActivity.this, "Please Wait", 
      "Processing...", true); 
    progressDialog.setCancelable(false); 
    progressDialog.setCanceledOnTouchOutside(false); 
} 

@Override 
public void hidePregress() { 
    progressDialog.dismiss(); 
} 

@Override 
public void setErrorOnEditText() { 
    /*Snackbar.make(v, msg, Snackbar.LENGTH_LONG) 
      .setAction("Action", null).show(); 
    showSuccessAlertBox(msg);*/ 
} 


@Override 
public void savedMsg(String msg) { 

} 

}

REGISTER_URLは、POSTメソッドを使用しています....とSTATE_URLは、GETメソッドを使用しています

...

+0

アダプタを設定したときに何が起こるかエラー –

+0

あなたの 'JSON'レスポンスを表示 – tahsinRupam

+0

私はコードをもう一度編集しました...上記の完全なコードを見ることができます.. –

答えて

0

まず代わりに受け入れることですJSONオブジェクトとしてのレスポンスは、あなた自身のカスタムPOJOクラスのインスタンスを使用します必要条件。次に、スピナーを配置するためのカスタムアダプタークラスを作成することができます。

上記のような小さな要件がある場合は、軽量で高速なVolleyの代わりにRetrofitを使用することをおすすめします。

関連する問題