Android的Java的HttpURLConnection的冻

Android的Java的HttpURLConnection的冻

本文介绍了Android的Java的HttpURLConnection的冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怀疑我遇到证实了我的简单的Andr​​oid应用程序线程的一些问题。

我有一个活动,它执行以下操作:


  1. 运行在主线程对话框

  2. 旋转后台线程,并击中一个PHP脚本使用retreiving数据 DefaultHttpClient ,然后关闭此连接。这成功返回一个URL的图片在互联网上

  3. 开启 HttpURLConnection类名为连接并尝试conn.connect

  4. 应用程序冻结在这一点上,没有误差

不知道什么code放到这里,我可以贴这一切,但将是太多,所以让我知道是否需要任何更多的信息:

  / **
 *背景异步任务以通过HTTP请求负载的所有问题
 * /
类LoadAllImages扩展的AsyncTask<字符串,字符串,字符串> {
  / **
   *启动后台线程显示进度对话框之前
   * * /
  @覆盖
  在preExecute保护无效(){
    super.on preExecute();
    //这里init'd对话框
  }  @覆盖
  保护字符串doInBackground(字符串参数... args){
    //大厦参数
    清单<&的NameValuePair GT; PARAMS =新的ArrayList<&的NameValuePair GT;();
    // URL从获取JSON字符串
    JSONObject的JSON = jParser.makeHtt prequest(url_question_details,GET,则params);    //检查JSON响应您的日志猫
    Log.d(所有问题,json.toString());    尝试{        发现//图片
        //获取问题阵
        照片= json.getJSONArray(TAG_IMAGES);        //通过所有问题循环
        的for(int i = 0; I< images.length();我++){
          JSONObject的C = images.getJSONObject(I)          //存储在变量中的每个JSON项目
          字符串ID = c.getString(TAG_IMAGEID);
          字符串位置= c.getString(TAG_IMAGELOCATION);          URL myFileUrl = NULL;
          尝试{
            myFileUrl =新的URL(位置);
          }赶上(MalformedURLException的E){
            // TODO自动生成catch块
            e.printStackTrace();
          }
          尝试{
            HttpURLConnection的康恩=(HttpURLConnection类)myFileUrl.openConnection();
            conn.setDoInput(真);
            conn.connect(); //这里冻结
            INT长度= conn.getContentLength();
            INT [] =的bitmapData新INT [长度]
            字节[] = bitmapData2新的字节[长度]
            InputStream为= conn.getInputStream();
          }赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
          }


解决方案

删除这两条线:

  conn.setDoInput(真);
conn.connect(); //这里冻结

当你这样做:

  HttpURLConnection的康恩=(HttpURLConnection类)myFileUrl.openConnection();
INT长度= conn.getContentLength();

您已经连接。调用 conn.getContentLength()引起 HttpURLConnection类来建立连接,发送请求并获取响应。你并不需要显式连接。你也不需要调用 setDoInput(真)因为这是默认设置。

I suspect I'm running into some issues borne out of threading in my simple Android app.

I have a Activity which does the following:

  1. runs a dialog on main thread
  2. spins a background thread and hits a PHP script for retreiving data using DefaultHttpClient and close this connection. This successfully returns a URL to a picture on the internet
  3. open HttpUrlConnection called conn and try conn.connect
  4. app freezes at this point with no error

Not sure what code to put here, I could paste it all but that would be too much so let me know if any more info is needed:

/**
 * Background Async Task to Load all Question by making HTTP Request
 */
class LoadAllImages extends AsyncTask<String, String, String> {
  /**
   * Before starting background thread Show Progress Dialog
   * */
  @Override
  protected void onPreExecute() {
    super.onPreExecute();
    // dialog init'd here
  }

  @Override
  protected String doInBackground(String... args) {
    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    // getting JSON string from URL
    JSONObject json = jParser.makeHttpRequest(url_question_details, "GET", params);



    // Check your log cat for JSON response
    Log.d("All Questions: ", json.toString());

    try {

        // images found
        // Getting Array of questions
        images = json.getJSONArray(TAG_IMAGES);

        // looping through All questions
        for (int i = 0; i < images.length(); i++) {
          JSONObject c = images.getJSONObject(i);

          // Storing each json item in variable
          String id = c.getString(TAG_IMAGEID);
          String location = c.getString(TAG_IMAGELOCATION);

          URL myFileUrl = null;
          try {
            myFileUrl = new URL(location);
          } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          try {
            HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
            conn.setDoInput(true);
            conn.connect(); // freezes here
            int length = conn.getContentLength();
            int[] bitmapData = new int[length];
            byte[] bitmapData2 = new byte[length];
            InputStream is = conn.getInputStream();
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
解决方案

Remove these 2 lines:

conn.setDoInput(true);
conn.connect(); // freezes here

When you do this:

HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
int length = conn.getContentLength();

You are already connected. Calling conn.getContentLength() causes HttpURLConnection to make the connection, sends the request and fetches the response. You don't need to explicitly connect. You also don't need to call setDoInput(true) since that is the default setting.

这篇关于Android的Java的HttpURLConnection的冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 05:28