http://172.16.7.203/sfAppServices/SF_UserLogin.svc/rest/login/
在这个用户名和密码是它的一部分,我需要发布用户从edittext输入的用户名和密码,并得到如下json响应
{"Result":"1","UserID":"0"}
如何使用改装获得json。我已使用asynctask成功完成此操作。但感觉很难通过改造来实现。我的问题是
以下是我的主要活动:
package com.example.first.servicefirst;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import retrofit.Callback;
import retrofit.GsonConverterFactory;
import retrofit.Response;
import retrofit.Retrofit;
public class MainActivity extends Activity {
EditText password,userName;
Button login,resister;
ProgressBar progressbar;
TextView tv;
String url="http://172.16.7.203/sfAppServices/SF_UserLogin.svc/rest/login/";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
password=(EditText) findViewById(R.id.password);
userName=(EditText) findViewById(R.id.txtEmployeeCode);
login=(Button) findViewById(R.id.btnsignin);
userName.setBackgroundResource(R.drawable.colorfoucs);
//progess_msz.setVisibility(View.GONE);
ConnectivityManager cn=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo nf=cn.getActiveNetworkInfo();
if(nf != null && nf.isConnected()==true )
{
Toast.makeText(this, "Network Available", Toast.LENGTH_LONG).show();
} else {
showAlertDialog(MainActivity.this,"No Network","Please Check Your Network Connectivity",true);
}
final ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
login.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
progressbar = (ProgressBar) findViewById(R.id.progressBar);
progressbar.setVisibility(View.VISIBLE);
login();
}
});
}
public void showAlertDialog(Context context, String title, String message, Boolean status) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
alertDialog.setTitle(title);
// Setting Dialog Message
alertDialog.setMessage(message);
// Setting alert dialog icon
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
// Showing Alert Message
alertDialog.show();
}
public void login(){
Retrofit retro = new
Retrofit.Builder().baseUrl("http://172.16.7.203").addConverterFactory(GsonConverterFactory.create()).build();
RetrofitRest retrofitRest = retro.create(RetrofitRest.class);
String s1 = userName.getText().toString();
String s2 = password.getText().toString();
if (s1.equals("")) {
userName.setError("Enter User Name");
}
if (s2.equals("")) {
password.setError("Enter Password");
}
retrofitRest.login(s1,s2, new Callback<ModelLogin>() {
@Override
public void onResponse(Response<ModelLogin> response, Retrofit retrofit) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
@Override
public void onFailure(Throwable t) {
}
});
}
}
下面是我的界面:
public interface RetrofitRest {
@FormUrlEncoded
@POST("/sfAppServices/SF_UserLogin.svc/rest/login/")
Callback<ModelLogin> login(@Query("empcode") String employeecode, @Query("password") String password,
Callback<ModelLogin> callback);
}
我的pojo:
package com.example.first.servicefirst;
import com.google.gson.annotations.SerializedName;
public class ModelLogin {
private String empcode;
private ModelLogin(){}
@SerializedName("Result")
private String result;
public String getResult(){
return result;
}
private String password;
public String getEmpcode() {
return empcode;
}
public String getPassword() {
return password;
}
public void setEmpcode(String empcode) {
this.empcode = empcode;
}
public void setPassword(String password) {
this.password = password;
}
}
在活动中编写以下代码
private void callLoginApi()
{
ApiListeners apiListener;
Retrofit retrofit = Retrofit.Builder().baseUrl("http://172.16.7.203/sfAppServices/SF_UserLogin.svc/rest/").build();
apiListener = retrofit.create(ApiListeners.class);
apiListener.loginUser("test@test.com", "123456",new Callback<String>()
{
@Override
public void success(String response, retrofit.client.Response arg1)
{
CommonUtills.dismissProgressDialog();
if(response != null && !response.isEmpty())
{
// Do what you want after getting success
}
}
@Override
public void failure(RetrofitError error)
{
// Do what you want after getting failure
}
});
}
回拨您可以根据需要更改此设置
您需要创建一个接口,在其中您需要创建登录API调用方法
@FormUrlEncoded
@POST("/login")
public void loginUser(@Field("email") String email,@Field("password") String password,retrofit.Callback<String> response);
由于请求参数是路径参数,因此请求不需要DTO。只需像这样为您的响应创建一个DTO。
public class LoginResponseDto {
@SerializedName("Result")
private String result;
@SerializedName("UserID")
private String userId;
// Getters & Setters etc.
}
并让我们解析URL并创建服务接口。我认为您的基本url将是“http://172.16.7.203/sfAppServices/“其他用于服务标识。这在很大程度上取决于您可能要使用的其他服务。如果您只是使用登录服务,您甚至可以这样创建您的基本url”http://172.16.7.203/sfAppServices/SF_UserLogin.svc“但这些决定会改变您的服务注释。
public interface LoginService{
@POST("/SF_UserLogin.svc/rest/login/{employeeCode}/{password}") // Assume your base url is http://172.16.7.203/sfAppServices/
public void login(@Path("employeeCode") String employeeCode, @Path("password") String password, Callback<LoginResponseDto> callback);
}
最后是您的改装实例。您不应该一次又一次地创建您的改装实例。而是在应用程序中创建它,或者使用单例模式,并在需要时获取登录服务。
由于您使用的是Retrofit 2,因此您应该使用这样的服务。
public interface LoginService{
@POST("/SF_UserLogin.svc/rest/login/{employeeCode}/{password}") // Assume your base url is http://172.16.7.203/sfAppServices/
public Call<LoginResponseDto> login(@Path("employeeCode") String employeeCode, @Path("password") String password);
}
以前版本的改造使用RestaAdapter,并提供了启用日志的功能。为什么在改型2.0中删除了该功能? 要启用日志,我必须。。 这是唯一的解决办法吗?以前的规定非常方便...
在我当前的项目中,我正在使用Maven和Spring。我目前正在使用SLF4J记录器来记录服务。代替它,我想使用OWASP-ESAPI记录器。我不想使用安全性,只是日志服务。有人能指导我如何使用记录器,用最少的努力替换slf4j记录器吗?我尝试了很多谷歌搜索,但没有任何帮助。我也会非常感谢一些链接来获得有关记录器的知识。
使用python代码从Goodreads获得书籍的描述,但是我的代码不允许我在开始实际搜索之前登录,我显然需要添加代码来使用我的用户名和pw进行登录,然后执行搜索。 现在发生的是,我首先得到了登录的弹出窗口,然后出现了错误: 请问有人能帮我一下吗?
我需要让 Web SSO SAML2 集成正常工作。最终目标是使用MS-ADFS作为idP - 出于测试目的,我创建了一个Okta帐户并在那里注册了一个Web应用程序。 我使用这个简单的SAML2 nuget包,https://github.com/jitbit/AspNetSaml 我有一个简单的Net core 3.1 webapp作为模板。 据我所知,认证请求进展顺利——我可以在Okta的日
我已经在Google cloud中创建了服务帐户,但我无法使用该帐户登录或执行其他操作。请帮帮我。 我需要在管理控制台中执行操作,我需要以G Suite域的超级管理员身份登录,也启用了Gsuite域范围的委派,但在尝试访问/登录服务帐户时仍然出错。 服务帐户登录错误
我正试图使用谷歌api购物(商家),我被这个问题卡住了。 我对php不是很熟悉,但对我来说,代码似乎很好,我想做的就是尝试从我的商店取货。我已经按照google建议的步骤在开发者控制台中设置了客户端id和客户端机密,但是在登录之后我仍然得到了错误。 下面是我使用的代码: 我得到以下错误: 0数组致命错误:未捕获异常“Google_Service_exception”,消息“{”error:{”er