私はAndroidアプリケーションで作業しています。今、私はログインシステムのいくつかのメソッドを書いています。サーバーの応答に従ってtrueまたはfalseを返すboolean
メソッドを作成しようとしています。ブールメソッドは何も返すことができません
public static boolean login(final String name, final String password) {
final boolean result;
Thread nt = new Thread() {
@Override
public void run() {
URL url;
HashMap<String, String> postDataParams = new HashMap<String, String>();
postDataParams.put("name", name);
postDataParams.put("password", password);
try {
url = new URL("");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
return true;
} else {
return false;
}
} catch (Exception e) {
return false;
e.printStackTrace();
}
}
};
nt.start();
}
問題がcannot return a value from a method with void result type
、何かが私は理解できないので、それが明確にboolean
メソッドとして定義されているため、すべてのreturn文は、強調表示されることをである:これは私のコードです。私は作成した新しいスレッドとは何かが関係していると思うが、このアプリケーションはAndroidデバイス上で実行されるので、別のスレッドでクエリを実行する必要がある。
ありがとうございます!
スレッドが終了するのを待つことができますし、スレッドからローカル変数を関数 'boolean login'に変更し、関数がwait文を渡した後に戻ります – HDJEMAI