我正在尝试开发一个android应用程序,我正在使用Retrofit2来处理我所有的网络内容以及与服务器的通信。 我有主要活动,也有登录活动。 我正在对主要活动初始化改造,并且尝试向登录活动发送改造初始化接口,然后事情变得很困难。 根据一些研究,我不能仅仅通过'.putextra(...)'函数将改造后的东西发送到新活动中。 我很乐意为此提供帮助。
我的主要活动:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final UserService userConnection;
userConnection = ApiUtils.getUserService();
Toolbar toolBar = (Toolbar)findViewById(R.id.app_bar);
toolBar.setTitle("Welcome @PlaceHolder");
setSupportActionBar(toolBar);
startActivity(new Intent(MainActivity.this,LoginActivity.class));
// Intent i = new Intent(this, LoginActivity.class);
// i.putExtra("userConnection", (Serializable) userConnection);
// startActivity(i);
ScrollView feed = (ScrollView)findViewById(R.id.feedView);
//LinearLayOut Setup
LinearLayout feed1 = (LinearLayout)findViewById(R.id.feed);
Feed feed2 = new Feed(this,feed1);
feed1 = feed2.getFeed();
//***************************************************//
Call call = userConnection.getResponse();
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
if(response.isSuccessful()){
ResObj resObj = (ResObj) response.body();
if(resObj.getpostID().equals("12Aa")){
//login start main activity
//Intent intent = new Intent(MainActivity.this, MainActivity.class);
// intent.putExtra("username", "tomer");
//startActivity(intent);
Log.e("Error is blabla: ","Yes");
} else {
Toast.makeText(MainActivity.this, "The username or password is incorrect", Toast.LENGTH_SHORT).show();
Log.e("Error is blabla: ",response.toString());
}
Log.e("Error is blabla: ",response.message().toString());
} else {
Toast.makeText(MainActivity.this, "Error! Please try again!", Toast.LENGTH_SHORT).show();
Log.e("Error is blabla: ",response.toString());
}
}
@Override
public void onFailure(Call call, Throwable t) {
Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
Log.e("Error is blabla: ",t.toString());
}
});
}
}
我的登录活动:
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Intent intent = getIntent();
final UserService userConnection =(UserService)getIntent.getSerializableExtra("userConnection");
Button loginBtn = (Button)findViewById(R.id.loginButton);
EditText emailBox = (EditText) findViewById(R.id.emailBox);
EditText passBox = (EditText) findViewById(R.id.passBox);
final String email = emailBox.getText().toString();
final String password = passBox.getText().toString();
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Call login = userConnection.login(email,password);
login.enqueue(new Callback() {
@Override
public void onResponse(Call call, retrofit2.Response response) {
if(response.isSuccessful())
{
ResObj resObj = (ResObj) response.body();
if(resObj.loginStatus().equals("connected")){
Log.e("Login Status: ","Success");
finish();
}
else {
Log.e("Login Status: ","Wrong Password or Email");
}
}
}
@Override
public void onFailure(Call call, Throwable t) {
Log.e("Login Status: ","Faild To Recieve Server Message");
}
});
}
});
Button registerBtn = (Button)findViewById(R.id.registerButton);
registerBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v){
startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
}
});
}
}
用户服务界面:
public interface UserService{
@GET("/testDB")
Call<ResObj> getResponse(
);
@POST("/login")
Call<ResObj> login(@Query("userEmail") String userEmail,
@Query("userPassword") String userPassword);
}
ApiUtiles类:
public class ApiUtils {
public static final String BASE_URL = "http://**.***.***.***:****";
public static UserService getUserService(){
return RetrofitClient.getClient(BASE_URL).create(UserService.class);
}
}
我很乐意获得有关如何在活动之间发送改造接口的解决方案或指南:)