如何在改造中添加添加参数

时间:2017-02-11 08:46:31

标签: java android parameters retrofit

我是Android开发的新手,并试图学习android改造。我试图从用户那里拿cnic并将其传递给参数。我尝试使用不同的方法,但没有使用它们

MainActivity.java

public class MainActivity extends AppCompatActivity {

EditText cnic;
String url = "https://Homeweb.com";


TextView text_number_1, text_city_2, text_status_1 ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    cnic=(EditText)findViewById(R.id.editText);
    text_status_1 = (TextView) findViewById(R.id.text_status_1);
    Button ButtonArray= (Button) findViewById(R.id.RetrofitArray);
    //Button ButtonObject= (Button) findViewById(R.id.RetrofitObject);

            ButtonArray.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
 //               View VisibleArray = findViewById(R.id.RetrofitArray);
                    //              VisibleArray.setVisibility(View.GONE);
 //                View VisibleObject = findViewById(R.id.RetrofitObject);
                    //              VisibleObject.setVisibility(View.GONE);
                    getRetrofitArray();
                }
            });



}




void getRetrofitArray() {

    //RetrofitArrayAPI.innerURL = "/Login.php?M_cnic=4956";
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

     RetrofitArrayAPI service = retrofit.create(RetrofitArrayAPI.class);

      //Call<List<Student>> call = service.getDetails();
      //Call<List<Student>> call = service.getDetails();
     //service.login( )
     Call<Student> call = service.getDetails();


    call.enqueue(new Callback<Student>() {
        @Override
        public void onResponse(Response<Student> response, Retrofit retrofit) {

            try {
                 String check;
                text_status_1.setText("StudentId  :  " + response.body().getC_name());
                         //text_status_1.setText("Status  :  " + StudentData.get(i). getC_name());
                         //text_city_2.setText("city  : " + StudentData.get(i).getCity());
                           check=response.body().getStatus();
                if(check.equals("1"))
                {
                    Toast.makeText(MainActivity.this, "Login In SuccessFully", Toast.LENGTH_SHORT).show();

                    Intent i = new Intent(MainActivity.this,Detail.class);

                    startActivity(i);


                }

                else if (check.equals("0"))  // login failure

                {

                    Toast.makeText(MainActivity.this, "Invalid UserName/Pass ", Toast.LENGTH_SHORT).show();

                }






            } catch (Exception e) {
                Log.d("onResponse", "There is an error");
                e.printStackTrace();
            }

        }

        @Override
        public void onFailure(Throwable t) {
            Log.d("onFailure", t.toString());
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
   }
 }

我现在直接通过cnic 的 ReforfitArray.java

   public interface RetrofitArrayAPI {

/*
 * Retrofit get annotation with our URL
 * And our method that will return us details of student.
*/


@GET("/Login.php?M_cnic=4956")

Call<Student>getDetails();


  }

1 个答案:

答案 0 :(得分:1)

基本上我试图将参数cnic添加到我的网址

我解决了我的问题 在我的界面

 Call<Student>getDetails(@Query("M_cnic") String M_cnic);

主要活动

    //RetrofitArrayAPI.innerURL = "/Login.php?M_cnic=4956";
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    RetrofitArrayAPI service = retrofit.create(RetrofitArrayAPI.class);
  String M_cnic = cnic.getText().toString();
    //Call<List<Student>> call = service.getDetails();
    //Call<List<Student>> call = service.getDetails();
    //service.login( )

   // M_cnic="4956";

    Call<Student> call = service.getDetails(M_cnic);