Java Android应用程序在setConnectionTimeout后崩溃
我想在我的Android应用程序中实现setConnectionTimeout。我遵循了几个代码结构,但无法使其工作。我的应用程序已连接到本地主机服务器,如果无法连接到数据库,我只希望它显示连接超时 以下是我的Http请求代码:Java Android应用程序在setConnectionTimeout后崩溃,java,android,apache,wamp,Java,Android,Apache,Wamp,我想在我的Android应用程序中实现setConnectionTimeout。我遵循了几个代码结构,但无法使其工作。我的应用程序已连接到本地主机服务器,如果无法连接到数据库,我只希望它显示连接超时 以下是我的Http请求代码: HttpParams httpParameters = new BasicHttpParams(); // Set the timeout in milliseconds until a connection is established. // The default
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
问题是当它达到3秒时,应用程序崩溃。在我实现这个片段之前,它只是冻结了更长的时间。如果我关闭Wifi,它会返回一个对话框,说互联网没有连接,这就是我想要实现的
如果数据库无法连接,您知道如何返回AlertDialogBox吗?(我已经为此禁用了WAMP)您的异常表示在解析JSON时存在空指针异常。您没有在这里附加该代码,所以我只假设一旦发生超时,您仍在尝试将输入流解析为JSON
最好先检查您的输入流是否为空。为crashlog发布您的日志猫。这是日志猫:请原谅我的初学者,但您能告诉我代码在哪里,以及如何检查我的输入流是否为空并使其为空吗?