当我从AynchTask调用REST-API时,它被定义为与活动相同的类中的私有(作为活动的内部类),它可以工作。
以下是代码:
protected String doInBackground(String... params){
EditText user = (EditText) findViewById(R.id.enter_username);
EditText pass = (EditText) findViewById(R.id.enter_password);
String username = user.getText().toString();
String password = pass.getText().toString();
String message = null;
HttpClient httpClient = ServiceHelper.getHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost("http://test.gogch.com/gch-restful/login");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username",username));
nameValuePairs.add(new BasicNameValuePair("password",password));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}
catch (UnsupportedEncodingException e){
e.printStackTrace();
}
try {
HttpResponse response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
Log.d("Http Post Response:" ,Integer.toString(statusCode));
message = EntityUtils.toString(response.getEntity());
Log.d("Http Post Response:" ,message);
}
catch(ClientProtocolException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
return message;
}
但是当同一个Asynch被定义为一个新的类时,它无法工作。这是代码。
protected String doInBackground(String... params) {
Log.d("doINSTARTED","Success");
String username = params[1].toString();
String password = params[2].toString();
String message = null;
Log.d("TAG","username:" + username + " password:" + password);
HttpClient httpClient = ServiceHelper.getHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost("http://test.gogch.com/gch-restful/login");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username",username));
nameValuePairs.add(new BasicNameValuePair("password",password));
Log.d("TAG","WORKING123");
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}
catch (UnsupportedEncodingException e){
e.printStackTrace();
}
try {
Log.d("TAG","YES");
HttpResponse response = httpClient.execute(httpPost);
Log.d("TAG","STILL WORKING");
int statusCode = response.getStatusLine().getStatusCode();
Log.d("Http Post Response:" ,Integer.toString(statusCode));
message = EntityUtils.toString(response.getEntity());
Log.d("Http Post Response:" ,message);
}
catch(ClientProtocolException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
return message;
}
这就是日志所说的内容。
04-02 13:10:09.761 30250-30307 / com.cinch.getvitalsapp W / System.err:java.net.UnknownHostException:无法解析主机&#34; test.gogch.com&#34;:否与主机名相关联的地址
怎么可能?当我在同一个类中定义AsycMethod时,它正在工作。我连接到服务器并获得响应文本。
04-03 00:19:17.935 4910-4910/com.cinch.getvitalsapp I/art﹕ Late-enabling -Xcheck:jni
04-03 00:19:18.241 4910-4928/com.cinch.getvitalsapp D/OpenGLRenderer﹕ Render dirty regions requested: true
04-03 00:19:18.250 4910-4910/com.cinch.getvitalsapp D/Atlas﹕ Validating map...
04-03 00:19:18.354 4910-4928/com.cinch.getvitalsapp I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/28/14, c33033c, Ia6306ec328
04-03 00:19:18.355 4910-4928/com.cinch.getvitalsapp I/OpenGLRenderer﹕ Initialized EGL, version 1.4
04-03 00:19:18.377 4910-4928/com.cinch.getvitalsapp D/OpenGLRenderer﹕ Enabling debug mode 0
04-03 00:19:29.119 4910-5046/com.cinch.getvitalsapp D/doINSTARTED﹕ Success
04-03 00:19:29.124 4910-5046/com.cinch.getvitalsapp D/TAG﹕ username:okay password:okay
04-03 00:19:29.138 4910-5046/com.cinch.getvitalsapp D/TAG﹕ WORKING123
04-03 00:19:29.143 4910-5046/com.cinch.getvitalsapp D/TAG﹕ YES
--------- beginning of crash
04-03 00:19:29.173 4910-5046/com.cinch.getvitalsapp E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #4
Process: com.cinch.getvitalsapp, PID: 4910
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
at java.net.InetAddress.lookupHostByName(InetAddress.java:451)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
at com.cinch.getvitalsapp.services.PostLoginData.doInBackground(PostLoginData.java:67)
at com.cinch.getvitalsapp.services.PostLoginData.doInBackground(PostLoginData.java:35)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
at libcore.io.Posix.android_getaddrinfo(Native Method)
at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
at java.net.InetAddress.lookupHostByName(InetAddress.java:438)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
at com.cinch.getvitalsapp.services.PostLoginData.doInBackground(PostLoginData.java:67)
at com.cinch.getvitalsapp.services.PostLoginData.doInBackground(PostLoginData.java:35)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: android.system.ErrnoException: android_getaddrinfo failed: EACCES (Permission denied)
at libcore.io.Posix.android_getaddrinfo(Native Method)
at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
at java.net.InetAddress.lookupHostByName(InetAddress.java:438)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
at com.cinch.getvitalsapp.services.PostLoginData.doInBackground(PostLoginData.java:67)
at com.cinch.getvitalsapp.services.PostLoginData.doInBackground(PostLoginData.java:35)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
04-03 00:19:29.460 4910-4928/com.cinch.getvitalsapp D/OpenGLRenderer﹕ endAllStagingAnimators on 0xb80f5cf0 (RippleDrawable) with handle 0xb8139ac8
04-03 00:19:32.157 4910-5046/com.cinch.getvitalsapp I/Process﹕ Sending signal. PID: 4910 SIG: 9