doudou0111 2017-12-21 08:30 采纳率: 0%
浏览 34

从android [duplicate]中的URL检索JSON数据时出现异常

This question already has an answer here:

This is the URL i am calling --

private String url="http://mywebsite.com/requiredfolder/myfile.php";

This is the json output getting from the above URL --

{"wallpapers":[{"id":"9","video_url":"http://mywebsite.com/AndroidApp/VideoUploads/raman.das@gmail.com_X5x6MVgv_VID-20171202-WA0012.mp4","downloads":"0","fav":"0"},
{"id":"8","video_url":"http://mywebsite.com/AndroidApp/VideoUploads/raman.das@gmail.com_eFmWtS@I_Funny Kid playing.mp4","downloads":"0","fav":"0"},
{"id":"7","video_url":"http://mywebsite.com/AndroidApp/VideoUploads/raman.das@gmail.com_trE3U&%x_VID_20171019_163735.mp4","downloads":"0","fav":"0"},
{"id":"6","video_url":"http://mywebsite.com/AndroidApp/VideoUploads/raman.das@gmail.com_ROgIfyHG_Assamesefunny.mp4","downloads":"0","fav":"0"},
{"id":"3","video_url":"http://mywebsite.com/AndroidApp/VideoUploads/raman.das@gmail.com_C2z$XU6!_Bombay Ka Brahman.mp4","downloads":"0","fav":"0"}],"success":1,"count":"5"}

Now in my Java file when trying to read the same using the given code --

try
        {
            DefaultHttpClient defaultClient = new DefaultHttpClient();
            Log.d("Message : ","DefaultHttpClient done");
            HttpGet httpGetRequest = new HttpGet(url);
            Log.d("Message : ","HttpGet done");
            HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
            Log.d("Message : ","HttpResponse done");
            BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
            Log.d("Message : ","BufferedReader done");

            result = reader.readLine();
            //response = CustomHttpClient.executeHttpGet(url);
            //Log.d("Message : ","response done");
            //result = response.toString();
            Log.d("Message : ","result done");
            json = new JSONObject(result);
         }
          catch (Exception e) {
            Log.e("log_tag","Error in http connection!!" + e.toString());
         }

In the above code, in line --

HttpResponse httpResponse = defaultClient.execute(httpGetRequest);

I am getting the runtime execption.

E/log_tag: Error in http connection!!android.os.NetworkOnMainThreadException

Struggled a lot but not getting any idea, any lights on my code will be great.

</div>
  • 写回答

2条回答 默认 最新

  • dongpa9277 2017-12-21 08:36
    关注

    NetworkOnMainThreadException

    The exception that is thrown when an application attempts to perform a networking operation on its main thread. Try implementing your logic in a different background thread.

    NetworkOnMainThreadException

    评论

报告相同问题?

悬赏问题

  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据