安卓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配置文件没有添加网络操作的权限。

解决方法

添加网络操作的权限

安卓getResponseCode()方法不走_第1张图片

阴影部分代码就是





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

565191947


你可能感兴趣的:(app安卓类)