AsyncTask运行时异常错误

时间:2018-07-24 14:12:36

标签: android android-asynctask

我制作了一个可以将图像和一些信息发送到服务器的应用程序。 当我测试应用程序时,有时会成功上传图像,有时会由于以下错误而崩溃:

java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at libcore.net.UriCodec.encode(UriCodec.java:132)
at java.net.URLEncoder.encode(URLEncoder.java:57)
at elder.msu.testupload.RequestHandler.getPostDataString(RequestHandler.java:61)
at elder.msu.testupload.RequestHandler.sendPostRequest(RequestHandler.java:30)
at elder.msu.testupload.MainActivity$1UploadImage.doInBackground(MainActivity.java:244)
at elder.msu.testupload.MainActivity$1UploadImage.doInBackground(MainActivity.java:204)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
... 4 more

我的doin背景:

    protected String doInBackground(Bitmap... params) {
         //Prepare data to send
            result = rh.sendPostRequest(Server.URLCreate_Game,data);
            return result;
        }

我的RequestHandler类:

 public String sendPostRequest(String URL, HashMap<String,String> data){
    StringBuilder sb = new StringBuilder();
    try {
        java.net.URL url=new URL(URL);
        HttpURLConnection connection= (HttpURLConnection) url.openConnection();
        connection.setReadTimeout(15000);
        connection.setConnectTimeout(15000);
        connection.setRequestMethod("POST");
        connection.setDoInput(true);
        connection.setDoOutput(true);
        OutputStream outputStream=connection.getOutputStream();
        BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
        writer.write(getPostDataString(data));
        writer.flush();
        writer.close();
        outputStream.close();
        int requestcode=connection.getResponseCode();
        BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));

        sb=new StringBuilder();
        String reponse;
        while ((reponse=reader.readLine())!=null){
            sb.append(reponse);
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return sb.toString();
}

private String getPostDataString(HashMap<String, String> data) throws UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();
    boolean first = true;
    for (Map.Entry<String, String> entry : data.entrySet()) {
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
    }

    return result.toString();

我只在这里输入一些内容,因为堆栈上的内容不允许我发布过多的代码来插入问题。

0 个答案:

没有答案