在android studio中,如何从与oracle数据库连接的servlet程序中获取其余api web服务url链接,以及如何为该程序连接oracle数据库
答案 0 :(得分:2)
这很容易,您可以使用HttpConnection
或诸如Retrofit之类的第三方库。我建议使用翻新。那太好了,很容易。
但是,您遇到的困难是创建RESTful API。因此,首先您需要学习使用Java创建RESTful API。我想您应该使用Framework Spring。
在此处详细了解:https://spring.io/guides/gs/rest-service/
一旦拥有了所有需要的RESTful API。您可以在自己的android项目中非常轻松地使用它。
首先创建您的API接口
public interface Api {
@FormUrlEncoded
@POST("createuser")
Call<DefaultResponse> createUser(
@Field("email") String email,
@Field("password") String password,
@Field("name") String name,
@Field("school") String school
);
@FormUrlEncoded
@POST("userlogin")
Call<LoginResponse> userLogin(
@Field("email") String email,
@Field("password") String password
);
@GET("allusers")
Call<UsersResponse> getUsers();
@FormUrlEncoded
@PUT("updateuser/{id}")
Call<LoginResponse> updateUser(
@Path("id") int id,
@Field("email") String email,
@Field("name") String name,
@Field("school") String school
);
@FormUrlEncoded
@PUT("updatepassword")
Call<DefaultResponse> updatePassword(
@Field("currentpassword") String currentpassword,
@Field("newpassword") String newpassword,
@Field("email") String email
);
@DELETE("deleteuser/{id}")
Call<DefaultResponse> deleteUser(@Path("id") int id);
}
创建要获取API实例的类。
public class RetrofitClient {
private static final String AUTH = "Basic " + Base64.encodeToString(("belalkhan:123456").getBytes(), Base64.NO_WRAP);
private static final String BASE_URL = "http://simplifiedlabs.xyz/MyApi/public/";
private static RetrofitClient mInstance;
private Retrofit retrofit;
private RetrofitClient() {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static synchronized RetrofitClient getInstance() {
if (mInstance == null) {
mInstance = new RetrofitClient();
}
return mInstance;
}
public Api getApi() {
return retrofit.create(Api.class);
}
}
然后使用此类执行API调用。
Call<LoginResponse> call = RetrofitClient
.getInstance().getApi().userLogin(email, password);
call.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
LoginResponse loginResponse = response.body();
if (!loginResponse.isError()) {
SharedPrefManager.getInstance(LoginActivity.this)
.saveUser(loginResponse.getUser());
Intent intent = new Intent(LoginActivity.this, ProfileActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
} else {
Toast.makeText(LoginActivity.this, loginResponse.getMessage(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
}
});
您需要互联网许可才能执行此操作。
如果您是初学者,那么这些步骤可能会使您感到困惑。因此,建议您在YouTube上观看此 Complete Retrofit Course 。本课程涵盖了从构建RESTful API到在Android项目中使用它的所有内容。每件事都包含所有细节的逐步说明。希望这会有所帮助。
答案 1 :(得分:0)
package com.javacodegeeks.snippets.core;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectToOracle {
public static void main(String[] args) {
Connection connection = null;
try {
// Load the Oracle JDBC driver
String driverName = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverName);
//Create a connection to the database
String serverName = "localhost";
String serverPort = "1521";
String sid = "mySchema";
String url = "jdbc:oracle:thin:@" + serverName + ":" + serverPort + ":" + sid;
String username = "username";
String password = "password";
connection = DriverManager.getConnection(url, username, password);
System.out.println("Successfully Connected to the database!");
} catch (ClassNotFoundException e) {
System.out.println("Could not find the database driver " + e.getMessage());
} catch (SQLException e) {
System.out.println("Could not connect to the database " + e.getMessage());
}
}
}