java.lang.RuntimeException:执行doInBackground()

时间:2015-11-11 23:20:47

标签: android multithreading android-asynctask runtimeexception

我正在尝试从网址获取JSON代码 当我尝试在浏览器上打开时,URL工作正常 但是当我运行应用程序时,它会崩溃并显示此日志:

11-11 21:06:30.489  25670-25695/app.com.example.android.popularmovies E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: app.com.example.android.popularmovies, PID: 25670
java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:300)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:841)
 Caused by: java.lang.NullPointerException
        at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
        at org.json.JSONTokener.nextValue(JSONTokener.java:94)
        at org.json.JSONObject.<init>(JSONObject.java:155)
        at org.json.JSONObject.<init>(JSONObject.java:172)
        at app.com.example.android.popularmovies.MainActivityFragment.getJSONResults(MainActivityFragment.java:137)
        at app.com.example.android.popularmovies.MainActivityFragment.getStringArrayFromJson(MainActivityFragment.java:94)
        at app.com.example.android.popularmovies.MainActivityFragment.access$1100(MainActivityFragment.java:30)
        at app.com.example.android.popularmovies.MainActivityFragment$FetchDataClass.doInBackground(MainActivityFragment.java:264)
        at app.com.example.android.popularmovies.MainActivityFragment$FetchDataClass.doInBackground(MainActivityFragment.java:249)
        at android.os.AsyncTask$2.call(AsyncTask.java:288)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)


这很奇怪,因为它工作,我不记得改变任何东西 这是我的AsyncTask:

    public class FetchDataClass extends AsyncTask<String, Void, String[]>{
    private final String IMG_URL = "http://image.tmdb.org/t/p/w185/";
    private final String JSON_PATH_ITEM = "poster_path";

    @Override
    protected String[] doInBackground(String... params){
        Uri.Builder builder = new Uri.Builder();
        builder.scheme("http")
                .authority("api.themoviedb.org")
                .appendPath("3")
                .appendPath("discover")
                .appendPath("movie")
                .appendQueryParameter("sort_by", params[0])
                .appendQueryParameter("api_key", API_KEY);
        jsonCode = dataFetcher(builder);
        return getStringArrayFromJson(jsonCode, JSON_PATH_ITEM);
    }

    @Override
    protected void onPostExecute(String[] result){
        if(result != null){
            adapter.clear();
            for(String dataStr : result) {
                String imgURL = dataStr;
                if(dataStr != "null") {
                    imgURL = IMG_URL + dataStr;
                }
                adapter.add(imgURL);
            }
        }
    }
}

方法“dataFecther”:

    private String dataFetcher(Uri.Builder uriBuilder){
    HttpURLConnection urlConnection = null;
    BufferedReader reader = null;

    String jsonDataStr = null;
    try{
        String myURL = uriBuilder.build().toString();
        URL url = new URL(myURL);
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        try{
            urlConnection.connect();
        }catch (NullPointerException e){
            return null;
        }

        InputStream inputStream = urlConnection.getInputStream();
        StringBuffer buffer = new StringBuffer();

        if(inputStream == null){
            return null;
        }

        reader = new BufferedReader(new InputStreamReader(inputStream));

        if(buffer.length() == 0){
            return null;
        }
        jsonDataStr = buffer.toString();
    }catch (IOException e){
        return null;
    }finally {
        if(urlConnection == null){
            urlConnection.disconnect();
        }
        if(reader != null){
            try {
                reader.close();
            }catch (final IOException e){
                return null;
            }
        }
    }
    return jsonDataStr;
}


private String[] getStringArrayFromJson(String json, String name){
    int resultLength = getJSONResults(json).length();
    String[] strArray = new String[resultLength];

    for(int i = 0; i < resultLength; i++){
        strArray[i] = getStringFromJson(json, i, name);
    }

    return strArray;
}

private String getStringFromJson(String json, int index, String name){
    String jsonStr = null;
    try {
        jsonStr = getJSONResults(json)
                .getJSONObject(index)
                .getString(name);
    }catch (JSONException e){
        return null;
    }
    if(jsonStr == "null" && name != "poster_path"){
        jsonStr = "Not avaliable.";
    }
    return jsonStr;
}
    private JSONArray getJSONResults(String json){
    final String JSON_RESULTS = "results";
    JSONArray results = null;

    try {
        JSONObject jsonObject = new JSONObject(json);
        results = jsonObject.getJSONArray(JSON_RESULTS);
    }catch (JSONException e){
        return null;
    }
    return results;
}



有谁知道我怎么能解决这个问题?谢谢!

1 个答案:

答案 0 :(得分:0)

我认为方法dataFetcher()返回null。你应该检查一下。