我正在尝试创建一个名为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
答案 0 :(得分:0)
我自己想出了问题。 应用程序无法连接到服务器的原因,因为我在我的PC上使用了不同的wifi连接(其中运行了USB服务器),并且在运行我的应用程序的手机上使用了不同的wifi连接。