Android 需要更新登录示例的帮助吗?
在此用户名和密码是其中的一部分,我需要发布用户从edittext输入的用户名和密码,并获得如下json响应Android 需要更新登录示例的帮助吗?,android,retrofit,Android,Retrofit,在此用户名和密码是其中的一部分,我需要发布用户从edittext输入的用户名和密码,并获得如下json响应 {"Result":"1","UserID":"0"} 如何使用改型获得json。我已成功使用asynctask完成此任务。但感觉通过改造很难实施。 我的问题是 哪个是基本url 在接口中,应在post方法中公开 如何通过json从rest服务获取json响应 以下是我的主要活动: package com.example.first.servicefirst; import
{"Result":"1","UserID":"0"}
如何使用改型获得json。我已成功使用asynctask完成此任务。但感觉通过改造很难实施。
我的问题是
- 哪个是基本url
- 在接口中,应在post方法中公开
- 如何通过json从rest服务获取json响应
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) {
}
});
}
}
}由于请求参数是路径参数,因此请求不需要DTO。只需为您的响应创建一个DTO,如下所示
public class LoginResponseDto {
@SerializedName("Result")
private String result;
@SerializedName("UserID")
private String userId;
// Getters & Setters etc.
}
让我们解析您的URL并创建您的服务接口。我认为您的基本url将是“”,其他url用于服务标识。这在很大程度上取决于您可能想要使用的其他服务。如果您只是使用登录服务,您甚至可以将您的基本url设置为“”这样,但这些决定会更改您的服务注释
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);
}
在活动中编写以下代码
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
}
});
}
private void callLoginApi()
{
ApiListeners apiListener;
改装改装=改装.Builder().baseUrl(“http://172.16.7.203/sfAppServices/SF_UserLogin.svc/rest/build();
apiListener=reformation.create(ApiListeners.class);
apilitener.loginUser(“test@test.com“,”123456“,新回调()
{
@凌驾
public void成功(字符串响应,reformation.client.response arg1)
{
CommonUtills.dismissProgressDialog();
if(response!=null&&!response.isEmpty())
{
//成功后做你想做的事
}
}
@凌驾
公共无效失败(错误)
{
//失败后做你想做的事
}
});
}
您可以根据需要更改此设置
您需要创建一个接口,在其中您需要创建登录API调用方法
@FormUrlEncoded
@POST("/login")
public void loginUser(@Field("email") String email,@Field("password") String password,retrofit.Callback<String> response);
@FormUrlEncoded
@POST(“/登录”)
public void登录用户(@Field(“email”)字符串email,@Field(“password”)字符串password,reformation.Callback response);
感谢您的回复bhdrkn我的帖子url将类似于服务:{EMPLOYEECODE}/{PASSWORD}@M.Yogeshwaran您所说的获取url是什么意思?在您的问题中,没有关于HTTP GET请求的信息。您可以在employeeCode中发布用户信息。我认为您是从编辑文本(用户名)中获取employeeCode。您确定您的url是“172.16.7.203/sfAppServices/SF_UserLogin.svc/rest/login{EMPLOYEECODE}/{P”ASSWORD}“这意味着您的用户名和密码是路径参数。您是否尝试过使用其他客户端(如Curl)登录?如果您发布您的curl,我可以提供更好的帮助。我尝试过,但出现了一些错误,例如无法为方法的翻新回调创建调用适配器翻新rest登录设置url,从某种意义上说,从服务器获取响应为json@M.Yogeshwaran您的Callack
课程包是什么?它应该是reformation
,(完整类名reformation.Callback
)。出现错误,例如无法为reformation Callback创建调用适配器
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);
}
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
}
});
}
@FormUrlEncoded
@POST("/login")
public void loginUser(@Field("email") String email,@Field("password") String password,retrofit.Callback<String> response);