JSONObjectRequest如何使用齐射

时间:2017-04-19 06:10:31

标签: android

我正在尝试从URl填充一个微调器,但是我无法使用GET方法将元素放到微调器上...我的POST方法很适合进行注册...但是无法将数据转换为微调器...这是我的代码

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._-]+@[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方法......

1 个答案:

答案 0 :(得分:0)

首先而不是接受响应,因为JSON对象根据您的要求使用您自己的自定义POJO类的实例。其次,您可以创建一个自定义适配器类来填充您的微调器。

如果您有如上所述的小要求,我建议您应该使用Retrofit而不是Volley,这是轻量级和快速的。