Я пытаюсь сделать функцию с именем authenticate, которая требует имени пользователя и пароля в качестве аргументов. Функция создает новый поток, публикует данные в файл .php и возвращает результат аутентификации.
Проблема, с которой я сталкиваюсь, заключается в том, что возникает IOException, и я не могу понять, почему. Я делюсь соответствующей функцией и LogCat здесь. Любая помощь будет высоко оценена.
Функция аутентификации:
private boolean authenticate (final String userName, final String password) throws ClientProtocolException, IOException, InterruptedException {
Thread httpThread = new Thread (new Runnable () {
@Override
public void run() {
HttpClient client = new DefaultHttpClient ();
HttpPost postHandle = new HttpPost (projectFolder + "appSupport/appBackEnd.php");
Log.d ("Connection",projectFolder + "appSupport/appBackEnd.php");
List <NameValuePair> credentials = new ArrayList <NameValuePair> ();
credentials.add (new BasicNameValuePair ("action", "authenticate"));
credentials.add (new BasicNameValuePair ("username", userName));
credentials.add (new BasicNameValuePair ("password", password));
try {
postHandle.setEntity(new UrlEncodedFormEntity(credentials));
Log.d ("Connection", "Entity Set!");
HttpResponse response = client.execute(postHandle);
Log.d ("Connection", "Executed!");
String res = response.getEntity().toString();
Log.d ("Connection", "Response" + res);
if (res.equals("1"))
result = true; // result is a global variable
else
result = false;
}
catch (UnsupportedEncodingException e) {
Log.d ("Connection", "Exception! UnsupportedEncodingException");
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
Log.d ("Connection", "Exception! ClientProtocolException");
e.printStackTrace();
} catch (IOException e) {
Log.d ("Connection", "Exception! IOException");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
httpThread.start();
httpThread.join();
return result;
}
LogCat:
01-12 23:17:33.566: D/PackageItemInfo(32301): loadIcon(cached)name=com.example.dummyproject.SignIn
01-12 23:17:33.636: D/dalvikvm(32301): GC_FOR_ALLOC freed 66K, 65% free 4474K/12760K, paused 23ms, total 23ms
01-12 23:17:33.666: I/dalvikvm-heap(32301): Grow heap (frag case) to 18.562MB for 12582928-byte allocation
01-12 23:17:33.696: D/dalvikvm(32301): GC_CONCURRENT freed 3K, 34% free 16759K/25052K, paused 2ms+2ms, total 31ms
01-12 23:17:34.327: D/libEGL(32301): loaded /system/lib/egl/libEGL_adreno200.so
01-12 23:17:34.327: D/libEGL(32301): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
01-12 23:17:34.337: D/libEGL(32301): loaded /system/lib/egl/libGLESv2_adreno200.so
01-12 23:17:34.347: I/Adreno200-EGL(32301): <qeglDrvAPI_eglInitialize:265>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_2.5.04.02.02.040.400_msm8960_JB_2.5_CL3744273_release_AU (CL3744273)
01-12 23:17:34.347: I/Adreno200-EGL(32301): Build Date: 06/30/13 Sun
01-12 23:17:34.347: I/Adreno200-EGL(32301): Local Branch:
01-12 23:17:34.347: I/Adreno200-EGL(32301): Remote Branch: quic/jb_2.5
01-12 23:17:34.347: I/Adreno200-EGL(32301): Local Patches: NONE
01-12 23:17:34.347: I/Adreno200-EGL(32301): Reconstruct Branch: AU_LINUX_ANDROID_JB_2.5.04.02.02.040.400 + NOTHING
01-12 23:17:34.427: D/OpenGLRenderer(32301): Enabling debug mode 0
01-12 23:17:38.381: D/Connection(32301): http://192.168.1.2:8080/submitted/appSupport/appBackEnd.php
01-12 23:17:38.381: D/Connection(32301): Entity Set!
01-12 23:17:38.431: D/Connection(32301): Exception! IOException
01-12 23:17:38.431: W/System.err(32301): org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.1.2:8080 refused
01-12 23:17:38.431: W/System.err(32301): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:191)
01-12 23:17:38.431: W/System.err(32301): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-12 23:17:38.431: W/System.err(32301): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-12 23:17:38.431: W/System.err(32301): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-12 23:17:38.431: W/System.err(32301): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-12 23:17:38.431: W/System.err(32301): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-12 23:17:38.431: W/System.err(32301): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-12 23:17:38.431: W/System.err(32301): at com.example.dummyproject.SignIn$3.run(SignIn.java:107)
01-12 23:17:38.431: W/System.err(32301): at java.lang.Thread.run(Thread.java:856)
01-12 23:17:38.442: W/System.err(32301): Caused by: java.net.ConnectException: failed to connect to /192.168.1.2 (port 8080): connect failed: ECONNREFUSED (Connection refused)
01-12 23:17:38.442: W/System.err(32301): at libcore.io.IoBridge.connect(IoBridge.java:118)
01-12 23:17:38.442: W/System.err(32301): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
01-12 23:17:38.442: W/System.err(32301): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
01-12 23:17:38.442: W/System.err(32301): at java.net.Socket.connect(Socket.java:847)
01-12 23:17:38.442: W/System.err(32301): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
01-12 23:17:38.442: W/System.err(32301): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:152)
01-12 23:17:38.442: W/System.err(32301): ... 8 more
01-12 23:17:38.442: W/System.err(32301): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
01-12 23:17:38.442: W/System.err(32301): at libcore.io.Posix.connect(Native Method)
01-12 23:17:38.442: W/System.err(32301): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
01-12 23:17:38.442: W/System.err(32301): at libcore.io.IoBridge.connectErrno(IoBridge.java:131)
01-12 23:17:38.442: W/System.err(32301): at libcore.io.IoBridge.connect(IoBridge.java:116)
01-12 23:17:38.442: W/System.err(32301): ... 13 more
01-12 23:17:38.442: D/DEBUG(32301): could not authenticate
Я сам понял проблему. Причина, по которой приложению не удалось подключиться к серверу так как я использовал другое соединение Wi-Fi на моем ПК (на котором работал USB-сервер) и другое соединение Wi-Fi на моем мобильном телефоне, на котором запущено мое приложение.
Других решений пока нет …