提问人:Lucia 提问时间:9/16/2023 最后编辑:Lucia 更新时间:9/16/2023 访问量:52
响应:logcat 中的 null
Response: null in logcat
问:
我正在尝试获取Nextcloud服务器中的所有用户ID。我有这个 Login.java。我可以在 Web 应用程序中正常调用 getNCUserIds,但在 Android 应用程序中出现错误。我检查logcat,这里是:
响应:null
响应不应为 null,因为至少有一个帐户是 admin。我试图记录状态代码,但它没有显示。 这是服务器端使用 PHP 的 API:
public function getNCUserIds() {
$users = \OC::$server->getUserManager()->search('');
$userIds = array_map(function($user) {
return $user->getUID();
}, $users);
return new JSONResponse(['users' => $userIds]);
}
这是我存储 JSON 的模型:
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class YourResponseModel {
@SerializedName("users")
@Expose
private List<String> users;
public List<String> getUsers() {
return users;
}
}
这是 Login.java。我调用一个 API 来检查我是否成功登录:
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.app.R;
import com.example.app.api.AndroidNCApi;
import com.example.app.api.ApiProvider;
import com.example.app.api.GsonConfig;
import com.example.app.database.model.Account;
import com.example.app.database.model.YourResponseModel;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientFactory;
import com.owncloud.android.lib.common.OwnCloudCredentials;
import com.owncloud.android.lib.common.OwnCloudCredentialsFactory;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.HttpException;
import retrofit2.Response;
import java.io.Reader;
import java.lang.reflect.Type;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Login extends AppCompatActivity {
private static final String TAG = "LoginActivity";
private Button loginButton;
private OwnCloudClient mClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
loginButton = findViewById(R.id.btnLogin);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = "admin";
String password = "admin";
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(new Runnable() {
@Override
public void run() {
performNetworkOperation(username, password);
}
});
}
});
}
private void performNetworkOperation(String username, String password) {
try {
Uri serverUri = Uri.parse(getString(R.string.url_server));
mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, Login.this, true);
mClient.setCredentials(
OwnCloudCredentialsFactory.newBasicCredentials(username, password)
);
AndroidNCApi api = ApiProvider.getClient(Login.this).create(AndroidNCApi.class);
Call<YourResponseModel> call = api.getNCUserIds();
call.enqueue(new Callback<YourResponseModel>() {
@Override
public void onResponse(Call<YourResponseModel> call, Response<YourResponseModel> response) {
Log.e(TAG, "Response: " + response.body());
if (response.isSuccessful()) {
YourResponseModel responseData = response.body();
Log.e(TAG, "responseData: " + responseData.toString());
if (responseData != null) {
List<String> userIDs = responseData.getUsers();
Log.e(TAG, "User IDs: " + userIDs);
if (userIDs != null && !userIDs.isEmpty()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Login.this, "Login successful", Toast.LENGTH_SHORT).show();
}
});
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Login.this, "No user IDs found", Toast.LENGTH_SHORT).show();
}
});
}
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Login.this, "Response is empty", Toast.LENGTH_SHORT).show();
}
});
}
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Login.this, "Login failed", Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
public void onFailure(Call<YourResponseModel> call, Throwable t) {
Log.e(TAG, "Error: " + t.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Login.this, "Network error", Toast.LENGTH_SHORT).show();
}
});
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
答: 暂无答案
评论