我是android studio和stackoverflow的新手。我cureently工作的一个项目,它是登录与改造,它与sql server数据库连接。我有一个错误消息显示,当我运行应用程序和登录,它显示***java.lang.illegalStateException:应为开始_OJECT,但在第1行第2列路径$***处为STRING
webapi代码:
public class userController : ApiController
{
[HttpGet]
public string getUser()
{
SqlConnection mainconn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("Select UserID, username, password from tblLogin", mainconn);
DataTable dt = new DataTable();
da.Fill(dt);
if(dt.Rows.Count > 0 )
{
return JsonConvert.SerializeObject(dt);
}
else
{
return "User Not Found";
}
}
[HttpGet]
public string getUser(string username, string password)
{
SqlConnection mainconn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("Select UserID, username, password from tblLogin where username = '" + username + "' and password = '" + password + "'", mainconn);
DataTable dt = new DataTable();
da.Fill(dt);
if(dt.Rows.Count > 0)
{
return JsonConvert.SerializeObject(dt);
}
else
{
return "User Not Found";
}
}
}
webapi模型:
public class Person
{
public decimal UserID { get; set; }
public string username { get; set; }
public string password { get; set; }
}
android LoginResponse:
public class LoginResponse implements Serializable {
private String UserID;
private String username;
private List<LoginRequest> loginRequests = null;
public String getUserID() {
return UserID;
}
public void setUserID(String userID) {
UserID = userID;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public List<LoginRequest> getLoginRequests(){
return loginRequests;
}
public void setLoginRequests(List<LoginRequest> loginRequests) {
this.loginRequests = loginRequests;
}
}
登录请求:
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
ApiClient:
public class ApiClient {
public static Retrofit getRetrofit() {
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build();
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("http://XXX.XX.XXX.X/webapi/")
.client(okHttpClient)
.build();
return retrofit;
}
public static UserService getService() {
UserService userService = getRetrofit().create(UserService.class);
return userService;
}
}
用户服务:
public interface UserService {
@GET("api/user/")
Call<LoginResponse> loginUser(@Query("username") String username, @Query("password") String password);
}
登录活动:
public class MainActivity extends AppCompatActivity {
Button btnLogin;
EditText edUsername, edPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLogin = findViewById(R.id.btnLogin);
edUsername = findViewById(R.id.etUsername);
edPassword = findViewById(R.id.etPassword);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(TextUtils.isEmpty(edUsername.getText().toString()) || TextUtils.isEmpty(edPassword.getText().toString())) {
String message = "All inputs required ..";
Toast.makeText(MainActivity.this,message,Toast.LENGTH_LONG).show();;
} else {
LoginRequest loginRequest = new LoginRequest();
loginRequest.setUsername(edUsername.getText().toString());
loginRequest.setPassword(edPassword.getText().toString());
loginUser(loginRequest);
}
}
});
}
public void loginUser(LoginRequest loginRequest) {
Call<LoginResponse> loginResponseCall = ApiClient.getService().loginUser(edUsername.getText().toString(), edPassword.getText().toString());
loginResponseCall.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
if(response.isSuccessful()) {
LoginResponse loginResponse = response.body();
startActivity(new Intent(MainActivity.this, Visitor.class).putExtra("data",loginResponse));
finish();
} else {
String message = "An error occurred please try again later... ";
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
String message = t.getLocalizedMessage();
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
});
}
}
webapi结果:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">[{"UserID":1.0,"username":"Steady","password":"test"}]</string>
我不知道我的代码的哪一部分出错了,我想让它成功登录到主菜单页面,有没有Maven知道如何解决这个错误?
1条答案
按热度按时间wfypjpf41#
我已经解决了这个问题,如果我改变了
string
到IHttpActionResult
在我的webApi,所以结果显示[{"UserID":1.0,"username":"Steady","password":"test"}]
WebAPI更新: