【发布时间】:2017-02-14 16:52:05
【问题描述】:
我有课
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 来防止抛出异常 Andy 关于我必须如何实现它的建议?
【问题讨论】:
-
添加完整的错误日志
-
出现什么异常?
-
这是错误日志 02/14 18:03:54: Launching app W/System: ClassLoader referenced unknown path: /data/data/com.example.lele.condomadv/lib Hot swapped changes , 活动重新启动 E/EGL_emulation: tid 4615: eglSurfaceAttrib(1174): 错误 0x3009 (EGL_BAD_MATCH) W/OpenGLRenderer: 无法在表面 0x9a6e0060 上设置 EGL_SWAP_BEHAVIOR,错误 = EGL_BAD_MATCH E/EGL_emulation: tid 4615: eglSurface30091 错误( EGL_BAD_MATCH) W/OpenGLRenderer:无法在表面 0x9aac73e0 上设置 EGL_SWAP_BEHAVIOR,错误=EGL_BAD_MATCH
-
对不起格式,我是新来的:(