我在我的android应用程序中使用'com.koushikdutta.async:androidasync:3.1.0'。我的命令如下:
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;
import com.koushikdutta.async.http.AsyncHttpClient;
import com.koushikdutta.async.http.AsyncHttpPost;
import com.koushikdutta.async.http.AsyncHttpResponse;
import com.koushikdutta.async.http.body.MultipartFormDataBody;
import androidx.annotation.Nullable;
public class NetworkRequests{
ir.ghandban.Address address = new ir.ghandban.Address();//where we are gonna hold our network request addresses
String LoginRequestAnswer = "";
public String Login(final String phoneNumber, final String password, Context context) {
AsyncHttpPost post = new AsyncHttpPost(address.address("login"));
post.setTimeout(8000);
MultipartFormDataBody body = new MultipartFormDataBody();
body.addStringPart("phoneNumber", phoneNumber);
body.addStringPart("Pass", password);
post.setBody(body);
AsyncHttpClient.getDefaultInstance().executeString(post, new AsyncHttpClient.StringCallback() {
@Override
public void onCompleted(final Exception e, AsyncHttpResponse source, String result) {
if (e != null) {
LoginRequestAnswer = "Error";
}
if (result != null) {
switch (result) {
case "yes_user":
LoginRequestAnswer = "yes_user";
break;
case "no_user":
LoginRequestAnswer = "no_user";
break;
case "null":
LoginRequestAnswer = "null";
break;
}
}
}
});
return LoginRequestAnswer;
}
}
这是我如何在另一个活动中使用它
private void login(final String phoneNumber, final String password) {
avi.smoothToShow();
final SharedPreferences sp = getSharedPreferences("User", 0);
NetworkRequests networkRequests = new NetworkRequests();
switch (networkRequests.Login(phoneNumber, password, getApplicationContext())) {
case "Error":
avi.smoothToHide();
Snackbar snackbar = Snackbar.make(rel, "خطا در برقراری اتصال با سرور !", Snackbar.LENGTH_LONG);
View view = snackbar.getView();
view.setBackgroundColor(getResources().getColor(R.color.buttonBackground1));
snackbar.show();
//e.printStackTrace();
break;
case "yes_user":
avi.smoothToHide();
Snackbar snackbar2 = Snackbar.make(rel, "شما با موفقیت وارد حساب کاربری خود شدید !", Snackbar.LENGTH_LONG);
View view2 = snackbar2.getView();
view2.setBackgroundColor(getResources().getColor(R.color.buttonBackground1));
snackbar2.show();
SharedPreferences.Editor edit = sp.edit();
edit.putString("phoneNumber", phoneNumber);
edit.putString("Pass", password);
edit.apply();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtra("From", "LoginActivity");
startActivity(intent);
finish();
break;
case "no_user":
avi.smoothToHide();
Snackbar snackbar3 = Snackbar.make(rel, "نام کاربری و یا رمز عبور وارد شده اشتباه است!", Snackbar.LENGTH_LONG);
View view3 = snackbar3.getView();
view3.setBackgroundColor(getResources().getColor(R.color.buttonBackground1));
snackbar3.show();
break;
case "null":
avi.smoothToHide();
Snackbar snackbar4 = Snackbar.make(rel, "موارد ورودی شما خالی میباشد!", Snackbar.LENGTH_LONG);
View view4 = snackbar4.getView();
view4.setBackgroundColor(getResources().getColor(R.color.buttonBackground1));
snackbar4.show();
break;
case "":
avi.smoothToHide();
Snackbar snackbar5 = Snackbar.make(rel, "خالی", Snackbar.LENGTH_LONG);
View view5 = snackbar5.getView();
view5.setBackgroundColor(getResources().getColor(R.color.buttonBackground1));
snackbar5.show();
break;
}
}
总是得到最后一个大小写"。我检查了请求和结果工作正常,但问题是LoginRequestAnswer有它的第一个值,当我返回它,这是",我怎么能使它正确?
我试图使另一个方法,这是一个空,持有所有网络相关的代码,并从公共字符串方法回调它…但结果是一样的……
NetworkRequests = new NetworkRequests();
你写了不可读的代码,因为人们无法看到你试图创建一个活动。
对不起,您不应该使用new操作符来创建活动实例。
你只能使用Intent。
你谈论一个公共方法是令人担忧的…
它是…
String Login()
使Login()无效
函数在onCompleted()被触发前返回。
最好添加一个接口或回调来处理onCompleted()的结果。