我正在尝试使用网络服务,但它不起作用,该代码有问题吗?
private DefaultHttpClient client = new DefaultHttpClient();
public String recebe(String url) {
HttpGet getRequisicao = new HttpGet(url);
try {
HttpResponse getResponse = client.execute(getRequisicao);
final int stateCode = getResponse.getStatusLine().getStatusCode();
if (stateCode != HttpStatus.SC_OK) {
Log.w(getClass().getSimpleName(), "Error " + stateCode + " for URL " + url);
return null;
}
HttpEntity getEntidadeResponsavel = getResponse.getEntity();
if (getEntidadeResponsavel != null) {
return EntityUtils.toString(getEntidadeResponsavel);
}
}
catch (IOException e) {
getRequisicao.abort();
Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
}
return null;
}
和
private ArrayList<ProjetoModel> receberListaProjetos() {
String url = construirEndereco();
String response = recebeHTTP.recebe(url);
Log.d(getClass().getSimpleName(), response);
return xmlParser.parseProjeto(response);
}
我收到了这个错误:
09-25 20:19:36.313: W/RecebeHTTP(584): Error for URL http://www.camara.gov.br/SitCamaraWS/Proposicoes.asmx/ListarProposicoes?sigla=PL&numero=&ano=2013&datApresentacaoIni=&datApresentacaoFim=&autor=&parteNomeAutor=&siglaPartidoAutor=&siglaUFAutor=&generoAutor=&codEstado=&codOrgaoEstado=&emTramitacao=
09-25 20:19:36.331: W/dalvikvm(584): threadid=12: thread exiting with uncaught exception (group=0x409961f8)
09-25 20:19:36.351: E/AndroidRuntime(584): FATAL EXCEPTION: AsyncTask #1
09-25 20:19:36.351: E/AndroidRuntime(584): java.lang.RuntimeException: An error occured while executing doInBackground()
09-25 20:19:36.351: E/AndroidRuntime(584): at android.os.AsyncTask$3.done(AsyncTask.java:278)
09-25 20:19:36.351: E/AndroidRuntime(584): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-25 20:19:36.351: E/AndroidRuntime(584): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-25 20:19:36.351: E/AndroidRuntime(584): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-25 20:19:36.351: E/AndroidRuntime(584): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-25 20:19:36.351: E/AndroidRuntime(584): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
09-25 20:19:36.351: E/AndroidRuntime(584): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-25 20:19:36.351: E/AndroidRuntime(584): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-25 20:19:36.351: E/AndroidRuntime(584): at java.lang.Thread.run(Thread.java:856)
09-25 20:19:36.351: E/AndroidRuntime(584): Caused by: java.lang.NullPointerException: println needs a message
09-25 20:19:36.351: E/AndroidRuntime(584): at android.util.Log.println_native(Native Method)
09-25 20:19:36.351: E/AndroidRuntime(584): at android.util.Log.d(Log.java:138)
09-25 20:19:36.351: E/AndroidRuntime(584): at com.mds.app.services.ProcuraProjeto.receberListaProjetos(ProcuraProjeto.java:25)
09-25 20:19:36.351: E/AndroidRuntime(584): at com.mds.app.services.ProcuraProjeto.procurar(ProcuraProjeto.java:13)
09-25 20:19:36.351: E/AndroidRuntime(584): at com.mds.app.view.MainActivity$PerformMovieSearchTask.doInBackground(MainActivity.java:109)
09-25 20:19:36.351: E/AndroidRuntime(584): at com.mds.app.view.MainActivity$PerformMovieSearchTask.doInBackground(MainActivity.java:1)
09-25 20:19:36.351: E/AndroidRuntime(584): at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-25 20:19:36.351: E/AndroidRuntime(584): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-25 20:19:36.351: E/AndroidRuntime(584): ... 5 more
09-25 20:19:36.672: W/IInputConnectionWrapper(584): showStatusIcon on inactive InputConnection
09-25 20:19:38.141: E/WindowManager(584): Activity com.mds.app.view.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41083688 that was originally added here
09-25 20:19:38.141: E/WindowManager(584): android.view.WindowLeaked: Activity com.mds.app.view.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41083688 that was originally added here
09-25 20:19:38.141: E/WindowManager(584): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:343)
09-25 20:19:38.141: E/WindowManager(584): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:245)
09-25 20:19:38.141: E/WindowManager(584): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:193)
09-25 20:19:38.141: E/WindowManager(584): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:118)
09-25 20:19:38.141: E/WindowManager(584): at android.view.Window$LocalWindowManager.addView(Window.java:537)
09-25 20:19:38.141: E/WindowManager(584): at android.app.Dialog.show(Dialog.java:274)
09-25 20:19:38.141: E/WindowManager(584): at android.app.ProgressDialog.show(ProgressDialog.java:116)
09-25 20:19:38.141: E/WindowManager(584): at android.app.ProgressDialog.show(ProgressDialog.java:104)
09-25 20:19:38.141: E/WindowManager(584): at com.mds.app.view.MainActivity.executarPesquisa(MainActivity.java:79)
09-25 20:19:38.141: E/WindowManager(584): at com.mds.app.view.MainActivity.access$0(MainActivity.java:77)
09-25 20:19:38.141: E/WindowManager(584): at com.mds.app.view.MainActivity$1.onClick(MainActivity.java:60)
09-25 20:19:38.141: E/WindowManager(584): at android.view.View.performClick(View.java:3480)
09-25 20:19:38.141: E/WindowManager(584): at android.view.View$PerformClick.run(View.java:13983)
09-25 20:19:38.141: E/WindowManager(584): at android.os.Handler.handleCallback(Handler.java:605)
09-25 20:19:38.141: E/WindowManager(584): at android.os.Handler.dispatchMessage(Handler.java:92)
09-25 20:19:38.141: E/WindowManager(584): at android.os.Looper.loop(Looper.java:137)
09-25 20:19:38.141: E/WindowManager(584): at android.app.ActivityThread.main(ActivityThread.java:4340)
09-25 20:19:38.141: E/WindowManager(584): at java.lang.reflect.Method.invokeNative(Native Method)
09-25 20:19:38.141: E/WindowManager(584): at java.lang.reflect.Method.invoke(Method.java:511)
09-25 20:19:38.141: E/WindowManager(584): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-25 20:19:38.141: E/WindowManager(584): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-25 20:19:38.141: E/WindowManager(584): at dalvik.system.NativeStart.main(Native Method)
我打印了异常 e 的消息并得到了这个:
09-25 20:38:01.741: W/System.err(740): java.net.UnknownHostException: Unable to resolve host "www.camara.gov.br": No address associated with hostname
09-25 20:38:01.771: W/System.err(740): at java.net.InetAddress.lookupHostByName(InetAddress.java:426)
09-25 20:38:01.771: W/System.err(740): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
09-25 20:38:01.771: W/System.err(740): at java.net.InetAddress.getAllByName(InetAddress.java:220)
09-25 20:38:01.781: W/System.err(740): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
09-25 20:38:01.781: W/System.err(740): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-25 20:38:01.781: W/System.err(740): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-25 20:38:01.781: W/System.err(740): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
09-25 20:38:01.781: W/System.err(740): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-25 20:38:01.792: W/System.err(740): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-25 20:38:01.792: W/System.err(740): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
09-25 20:38:01.792: W/System.err(740): at com.mds.app.services.RecebeHTTP.recebe(RecebeHTTP.java:24)
09-25 20:38:01.792: W/System.err(740): at com.mds.app.services.ProcuraProjeto.receberListaProjetos(ProcuraProjeto.java:24)
09-25 20:38:01.792: W/System.err(740): at com.mds.app.services.ProcuraProjeto.procurar(ProcuraProjeto.java:13)
09-25 20:38:01.792: W/System.err(740): at com.mds.app.view.MainActivity$PerformMovieSearchTask.doInBackground(MainActivity.java:109)
09-25 20:38:01.822: W/System.err(740): at com.mds.app.view.MainActivity$PerformMovieSearchTask.doInBackground(MainActivity.java:1)
09-25 20:38:01.822: W/System.err(740): at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-25 20:38:01.822: W/System.err(740): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-25 20:38:01.831: W/System.err(740): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-25 20:38:01.831: W/System.err(740): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
09-25 20:38:01.831: W/System.err(740): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-25 20:38:01.842: W/System.err(740): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-25 20:38:01.842: W/System.err(740): at java.lang.Thread.run(Thread.java:856)
09-25 20:38:01.842: W/System.err(740): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
09-25 20:38:01.873: W/System.err(740): at libcore.io.Posix.getaddrinfo(Native Method)
09-25 20:38:01.873: W/System.err(740): at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
09-25 20:38:01.873: W/System.err(740): at java.net.InetAddress.lookupHostByName(InetAddress.java:411)
09-25 20:38:01.873: W/System.err(740): ... 21 more