安卓getResponseCode()方法不走

代码如下:

protected String doInBackground(String... params) {
				StringBuffer buffer = new StringBuffer();

				try {
					URL url = new URL(params[0]);
					HttpURLConnection conn = (HttpURLConnection) url.openConnection();
					conn.setRequestMethod("GET");

					int code = conn.getResponseCode();
					
					
					if (code == 200) {
						InputStreamReader reader = new InputStreamReader(conn.getInputStream(), "UTF-8");
						char[] arr = new char[1024 * 8];
						int len = 0;
						while ((len = reader.read(arr)) != -1) {
							String arrnew = new String(arr, 0, len);
							buffer.append(arrnew);
						}
					}

				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					Log.d("tag", "MalformedURLException");
					e.printStackTrace();
				} catch (ProtocolException e) {
					// TODO Auto-generated catch block
					Log.d("tag", "ProtocolException");
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					Log.d("tag", "IOException");
					e.printStackTrace();
				}

				
				return buffer.toString();
			}

			@Override
			protected void onPostExecute(String result) {

				Gson gs = new GsonBuilder().disableHtmlEscaping().create();
				
				
				
				
				try {
					JSONObject jsonong = new JSONObject(result);
					JSONArray listjson = jsonong.getJSONArray("list");
					
					for (int i = 0; i < listjson.length(); i++) {
						 news_model a_news = new news_model();
						 a_news.setImage_url(listjson.getJSONObject(i).getString("image_url"));
						 a_news.setInfor_url(listjson.getJSONObject(i).getString("url"));
						 a_news.setNews_title(listjson.getJSONObject(i).getString("title"));
						 a_news.setNews_author(listjson.getJSONObject(i).getString("author"));
						 a_news.setUpdate_time(listjson.getJSONObject(i).getString("publication_date"));
						 news_list.add(a_news);
						 Log.d("tag",   a_news.getNews_title()+"tag");
					}
					
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				
				
				adapt.notifyDataSetChanged();
				data_count = adapt.getCount();
				load_more_view.setVisibility(View.GONE);
				
			}

		}.execute(
				inter_url);




调试了两天。。打log发现

int code = conn.getResponseCode();

这句代码没执行。

始终不知道哪里错了,以为是服务器端未开启,第二天起来还是不走,然后仔细一看,发现项目的AndroidManifest.xml配置文件没有添加网络操作的权限。

解决方法

添加网络操作的权限


阴影部分代码就是





更多问题,欢迎加群讨论:qq群 :

565191947


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值