急:connection.connect有问题
手机能上网,Manifest.xml已经申请网络访问。关键代码://利用Get方法访问网络数据
private void requestDataByGet() {
try {
//新建URL对象
URL url = new URL("http://www.imooc.com/api/shopping?type=11");
//获取url的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置连接属性:超时30s自动不再访问网址
connection.setConnectTimeout(30 * 1000);
//设置连接属性:请求访问
connection.setRequestMethod("GET");
//设置获取数据格式类型jason
//设置获取字符集类型UTF-8
connection.setRequestProperty("Content-Type", "application/jason");
connection.setRequestProperty("Charset", "UTF-8");
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.connect();//发起连接
int responseCode = connection.getResponseCode();//获取连接码
String responseMessage = connection.getResponseMessage();//获取连接信息
//访问URL成功代码 HttpURLConnection.HTTP_OK=200
if (responseCode == HttpURLConnection.HTTP_OK)//如果访问成功
{
//获取连接的输入流
InputStream inputStream = connection.getInputStream();
//把输入流转换成String字符串
result = streamToString(inputStream);
//runOnUiThread帮助转到当前操作线程,也就是主线程
runOnUiThread(new Runnable() {
@Override
public void run() {
result = decode(result);//把结果转成UTF-8格式
}
});
} else//获取网络数据不成功
{
//TODO:error fail
Log.e(TAG, "run:error code" + responseCode + ",message:" + responseMessage);
}
} catch (MalformedURLException e)//捕获异常:不可返的URL地址
{
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
运行提示:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.administrator.gridviewtuangoudemo/com.example.administrator.gridviewtuangoudemo.MainActivity}: android.os.NetworkOnMainThreadException
at com.example.administrator.gridviewtuangoudemo.MainActivity.requestDataByGet(MainActivity.java:130)
也就是connection.connect连接失败。
断点在connection.setRequestMethod("GET"); 调试数据:this = {MainActivity@830047525032} url = {URL@830048396776} "http://www.imooc.com/api/shopping?type=11" connection = {HttpURLConnectionImpl@830048558736} "com.android.okhttp.internal.http.HttpURLConnectionImpl:http://www.imooc.com/api/shopping?type=11" client = {OkHttpClient@830048400504} selectedProxy = null httpEngine = null httpEngineFailure = null rawRequestHeaders = {RawHeaders@830048559264} redirectionCount = 0 fixedContentLength = -1 responseMessage = null method = "GET" fixedContentLengthLong = -1 instanceFollowRedirects = true HttpURLConnection.fixedContentLength = -1 responseCode = -1 chunkLength = -1 url = {URL@830048396776} "http://www.imooc.com/api/shopping?type=11" defaultHandler = {URLConnection$DefaultContentHandler@830048558872} contentType = null connected = false lastModified = -1 ifModifiedSince = 0 doOutput = false doInput = true connectTimeout = 0 readTimeout = 0 allowUserInteraction = false useCaches = true
断点设connection.connect,调试参数: connection = {HttpURLConnectionImpl@830048579184} "com.android.okhttp.internal.http.HttpURLConnectionImpl:http://www.imooc.com/api/shopping?type=11" this = {MainActivity@830047545480} url = {URL@830048417224} "http://www.imooc.com/api/shopping?type=11" connection = {HttpURLConnectionImpl@830048579184} "com.android.okhttp.internal.http.HttpURLConnectionImpl:http://www.imooc.com/api/shopping?type=11"
断点设int responseCode = connection.getResponseCode();
就出现如上错误提示。
因为有错误,手机调试就是闪屏,出不来东西,无法知道结果。我正在做3-2作业题
正在回答 回答被采纳积分+1
找到原因了,get 方法必须在异步线程中完成,不能在主线程使用
- 参与学习 人
- 提交作业 307 份
- 解答问题 1613 个
本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星