我有一个类的Android HttpURLConnection类总是抛出异常
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.lang.Exception;
import java.net.HttpURLConnection;
public class HttpURLConnectionExample {
private final String USER_AGENT = "Mozilla 5.0";
// HTTP GET request
public ArrayList<dispencer> sendGet() throws Exception {
String url = "http://192.168.1.9";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
ArrayList<dispencer> dispList = new ArrayList<dispencer>();
String[] strRows = response.toString().split("@");
for (int i= 0; i <strRows.length; i++) {
dispencer disp = new dispencer(strRows[i]);
if (!disp.isOk) continue;
dispList.add(disp);
System.out.println(disp.indirizzo);
}
return dispList;
}
}
我调用该方法sendGet
这样。
HttpURLConnectionExample httpFnd= new HttpURLConnectionExample();
ArrayList<dispencer> listAr = new ArrayList<dispencer>();
try{
listAr = httpFnd.sendGet();
}
catch (Exception Exception){
fndLayout.setVisibility(View.GONE);
System.out.println("exception handled");
}
每当我试着打电话sendGet我总是得到处理的消息例外,从来没有收到我的arrayList
回来,就像是从来没有建立的连接。 当我调用该方法我也收到此错误
d/NetworkSecurityConfig:没有网络安全配置的规定,使用 平台默认
,难道是我的问题的原因是什么?
感谢您的帮助。
编辑我看到我需要使用asyncTask来预防抛出异常安迪关于我如何实现它的建议?
添加完整的错误日志 – Jayanth
什么例外是trhown ? –
这是错误日志02/14 18:03:54:启动应用程序 W/System:ClassLoader引用未知路径:/data/data/com.example.lele.condomadv/lib 热交换更改,活动重新启动 E/EGL_emulation:TID 4615:eglSurfaceAttrib(1174):错误0x3009(EGL_BAD_MATCH) W/OpenGLRenderer:无法设置EGL_SWAP_BEHAVIOR上表面0x9a6e0060,误差= EGL_BAD_MATCH E/EGL_emulation:TID 4615:eglSurfaceAttrib(1174):错误0x3009(EGL_BAD_MATCH) W/OpenGLRenderer:无法设置表面上的EGL_SWAP_BEHAVIOR 0x9aac73e0,错误= EGL_BAD_MATCH –