也许这是一个愚蠢的问题,但我无法解决它。
我有一个填充列表视图的Web服务。但是,当没有网络连接时,我希望应用程序执行其他操作,因此,没有Web服务通信。
我知道我必须捕获一个ConnectException,但无论我在哪里放置try catch块它都不会捕获异常,因为我的logcat确实显示了异常。
以下是代码,您可以在下面找到logcat。
有人可以就如何放置try catch thingy提供一些建议吗?
提前致谢!
package com.android.trucka.directorio;
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ActividadPrincipal extends Activity {
private ListView list;
String[] listado = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_actividad_principal);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setTitle("Directorio");
list = (ListView) findViewById(R.id.lsvDirectorio);
list.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long id) {
String seleccionado = (String) ((TextView) arg1).getText();
seleccionado = seleccionado.replaceAll(" ", "");
if (VerificaNumero(seleccionado) == "SI") {
// Toast.makeText(getApplicationContext(), seleccionado,
// Toast.LENGTH_LONG).show();
Intent intentLlamada = new Intent(Intent.ACTION_CALL);
intentLlamada.setData(Uri.parse("tel:" + seleccionado));
intentLlamada.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intentLlamada);
}
}
});
Thread nt = new Thread() {
String res;
@Override
public void run() {
String NAMESPACE = "http://android.trucka.com/";
String URL = "http://192.168.9.86/SumadorWS/ServicioAndroid.asmx";
String SOAP_ACTION = "http://android.trucka.com/ObtenerDirectorio";
String METHOD_NAME = "ObtenerDirectorio";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE transporte = new HttpTransportSE(URL);
try {
transporte.call(SOAP_ACTION, envelope);
SoapPrimitive resultado_xml = (SoapPrimitive) envelope
.getResponse();
res = resultado_xml.toString();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
// System.out.println("RESPUESTA: " + res);
listado = res.split(",");
// for (int x = 0; x < arreglo.length; x++)
// System.out.println("[" + x + "]" + arreglo[x]);
runOnUiThread(new Runnable() {
@Override
public void run() {
ArrayAdapter<String> adaptador = new ArrayAdapter<String>(
getApplicationContext(),
android.R.layout.simple_list_item_1, listado);
list.setAdapter(adaptador);
// Toast.makeText(getApplicationContext(), res,
// Toast.LENGTH_LONG).show();
}
});
}
};
nt.start();
}
public static String VerificaNumero(String cadena) {
String resultado = null;
if (cadena.length() > 10) {
resultado = "NO";
} else if (cadena.length() <= 10) {
resultado = "SI";
}
return resultado;
}
}
06-25 11:21:49.140: W/System.err(27720): java.net.ConnectException: failed to connect to /192.168.9.86 (port 80): connect failed: EHOSTUNREACH (No route to host)
06-25 11:21:49.140: W/System.err(27720): at libcore.io.IoBridge.connect(IoBridge.java:114)
06-25 11:21:49.140: W/System.err(27720): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
06-25 11:21:49.140: W/System.err(27720): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:460)
06-25 11:21:49.150: W/System.err(27720): at java.net.Socket.connect(Socket.java:832)
06-25 11:21:49.150: W/System.err(27720): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
06-25 11:21:49.150: W/System.err(27720): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
06-25 11:21:49.150: W/System.err(27720): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
06-25 11:21:49.160: W/System.err(27720): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
06-25 11:21:49.160: W/System.err(27720): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
06-25 11:21:49.160: W/System.err(27720): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
06-25 11:21:49.160: W/System.err(27720): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
06-25 11:21:49.170: W/System.err(27720): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
06-25 11:21:49.170: W/System.err(27720): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
06-25 11:21:49.170: W/System.err(27720): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
06-25 11:21:49.170: W/System.err(27720): at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:46)
06-25 11:21:49.180: W/System.err(27720): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:68)
06-25 11:21:49.180: W/System.err(27720): at com.android.trucka.directorio.ActividadPrincipal$2.run(ActividadPrincipal.java:77)
06-25 11:21:49.180: W/System.err(27720): Caused by: libcore.io.ErrnoException: connect failed: EHOSTUNREACH (No route to host)
06-25 11:21:49.190: W/System.err(27720): at libcore.io.Posix.connect(Native Method)
06-25 11:21:49.190: W/System.err(27720): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
06-25 11:21:49.200: W/System.err(27720): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
06-25 11:21:49.200: W/System.err(27720): at libcore.io.IoBridge.connect(IoBridge.java:112)
06-25 11:21:49.200: W/System.err(27720): ... 16 more
06-25 11:21:49.200: W/dalvikvm(27720): threadid=11: thread exiting with uncaught exception (group=0x41316460)
06-25 11:21:49.210: E/AndroidRuntime(27720): FATAL EXCEPTION: Thread-678
06-25 11:21:49.210: E/AndroidRuntime(27720): java.lang.NullPointerException
06-25 11:21:49.210: E/AndroidRuntime(27720): at com.android.trucka.directorio.ActividadPrincipal$2.run(ActividadPrincipal.java:88)
答案 0 :(得分:0)
有时HttpTransportSE
课程会引发null
或NullPointerException
,您无法使用当前代码看到它。
在堆栈跟踪的最后一行,您可以看到第二个匿名内部类(线程)的NullPointerException
方法抛出Run
。
使用W/
似乎logcat中的第一个异常只是一个警告...
W/System.err(27720): java.net.ConnectException:
试试这个:
}catch (IOException e) {
e.printStackTrace();
}catch (XmlPullParserException e) {
e.printStackTrace();
}catch(Throwable t){
// Here you might see your exception
if(t != null)
e.printStackTrace();
}
答案 1 :(得分:0)
添加了一种检查连接性的方法
public static String VerificarConectividad() {
String resultado = null;
try {
Socket socket = new Socket();
SocketAddress direccion = new InetSocketAddress("192.168.10.135",
80);
socket.connect(direccion, 3000);
socket.close();
resultado = "CONECTADO";
} catch (IOException e) {
resultado = "DESONECTADO";
}
return resultado;
}