Android 套接字已关闭-从API下载

Android 套接字已关闭-从API下载,android,android-asynctask,Android,Android Asynctask,我正在做的是每X秒下载一个包含AsyncTask的文件列表: 首先是一个视频和图像列表,然后我下载每一个。一切都很完美 package com.example.tvrplayer; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.In

我正在做的是每X秒下载一个包含AsyncTask的文件列表:

首先是一个视频和图像列表,然后我下载每一个。一切都很完美

package com.example.tvrplayer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class updateFiles extends AsyncTask<Object, Integer, Long> {

     public PlayerActivity activity;

     public updateFiles(PlayerActivity a) { activity = a;}



    @Override
    protected Long doInBackground(Object... params) {
        Context context = (Context) params[0];
        String username = (String) params[1];
        String linkid = (String) params[2];
        String apiurl = (String) params[3];

        JSONArray programs = Json.getJson(apiurl + "/rest/program/device/"+ linkid +"/"+ username +"/"+ activity.deviceid, "GET");
        File mediadir = context.getDir("tvr", Context.MODE_PRIVATE);
            try {
                LINE 44- for (int j=0; j < programs.length(); j++) { 

                    JSONObject json_data = programs.getJSONObject(j);
                    String name = json_data.getString("Name").toLowerCase();
                    name = name.replace("-", "_");
                    if (name.contains("mp4") || name.contains("m4v") || name.contains("png") || name.contains("jpg") || name.contains("jpeg")) {
                        String _name = name.replace("/", "");
                        File file = new File(mediadir, _name);
                        Log.i("FILE SIZE", ""+file.length());
                        if ( file.length() > 0 ) {
                            Log.i("FILE EXISTS", _name);
                        } else {
                            Log.i("START DOWNLOAD", _name);
                            new Download().execute(context, _name, apiurl + "/rest/transfer/"+ linkid +"/"+ username +"/" + json_data.getString("ID"));
                        }
                        Log.i("FILES found", ""+mediadir.listFiles().length);
                    }
                }
        } catch (JSONException e) {
            Log.e("JSON Exception", e.getMessage());
            e.printStackTrace();
        }
//      activity.filelist = mediadir.listFiles();
        return null;
    }

    @Override
    protected void onPostExecute(Long result) {}

}
这是从API获取内容的JSON文件

package com.example.tvrplayer;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class Json {

    static JSONArray getJson(String url, String method) {
        // Log.i("JSON",url);

        InputStream is = null;
        String result = "";
        JSONArray jsonObject = null;

        // HTTP
        try {
            HttpClient httpclient = new DefaultHttpClient(); // for port 80 requests!
            if ( method == "GET") {
                HttpGet httpget = new HttpGet(url);
                HttpResponse response = httpclient.execute(httpget);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
                if (entity != null) {
                    httpget.abort();
                }
            } else if (method == "POST") {
                HttpPost httppost = new HttpPost(url);
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
            }



        } catch(Exception e) {
            Log.e("JSON - 1 -", e.toString());
            return null;
        }

        // Read response to string
        try {           
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();    
//          Log.d("JSON result",result);
        } catch(Exception e) {
            Log.e("JSON - 2 -", e.toString() + " - " + url);
            return null;
        }

        // Convert string to object
        try {
            jsonObject = new JSONArray(result);            
        } catch(JSONException e) {
            try {
                jsonObject = new JSONArray("["+result+"]");            
            } catch(JSONException e1) {
                Log.e("JSON - 3 -", e1.toString());
                return null;
            }
        }

        return jsonObject;
    }
}
所以我想象的是,当asynctask忙于下载时,另一个下载完成并关闭了连接?


你知道这是什么或者如何解决吗?

你在
doBackground()
方法的第44行得到了一个
空指针异常。请张贴完整的代码,以便我们可以进一步帮助您。确保在使用任何对象之前完成所有初始化。在任何情况下,检查第44行,无论您在那里使用的是哪一个对象,都要确保它不为NULL。

doInBackground()的第44行是什么?mm,它的for(int j=0;jBufferedReader=new BufferedReader(新的InputStreamReader(是“utf-8”),8);
不是很好,因为8不足以作为缓冲区,请尝试8192。如果我删除此Log.e(“JSON-2-”,e.toString()+“-”+url);返回null;它将继续运行,不会使应用程序崩溃。但我想这不是一个好主意?
程序
为null。请确保设置正确。JSON是否返回正确的值?还是返回null?我返回一个coreect值是的,带一个exeption的Execpt。它返回null。我是否应该将其改为返回空JSON数组?
getJson
存在问题,您需要修复它。它陷入异常并返回
null
,这反过来会导致第44行中
程序的NPE。
package com.example.tvrplayer;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class Json {

    static JSONArray getJson(String url, String method) {
        // Log.i("JSON",url);

        InputStream is = null;
        String result = "";
        JSONArray jsonObject = null;

        // HTTP
        try {
            HttpClient httpclient = new DefaultHttpClient(); // for port 80 requests!
            if ( method == "GET") {
                HttpGet httpget = new HttpGet(url);
                HttpResponse response = httpclient.execute(httpget);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
                if (entity != null) {
                    httpget.abort();
                }
            } else if (method == "POST") {
                HttpPost httppost = new HttpPost(url);
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
            }



        } catch(Exception e) {
            Log.e("JSON - 1 -", e.toString());
            return null;
        }

        // Read response to string
        try {           
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();    
//          Log.d("JSON result",result);
        } catch(Exception e) {
            Log.e("JSON - 2 -", e.toString() + " - " + url);
            return null;
        }

        // Convert string to object
        try {
            jsonObject = new JSONArray(result);            
        } catch(JSONException e) {
            try {
                jsonObject = new JSONArray("["+result+"]");            
            } catch(JSONException e1) {
                Log.e("JSON - 3 -", e1.toString());
                return null;
            }
        }

        return jsonObject;
    }
}