首页 文章

访问多部分上载的共享首选项时,Android上的Java错误

提问于
浏览
0

我需要这个问题的帮助 . 我正在尝试将文件上传到目前正常工作的远程服务器 . 我还可以将用户电子邮件保存为共享首选项 . 但是在尝试在异步任务中访问电子邮件时 . 它一直给出错误:

E / AndroidRuntime:FATAL EXCEPTION:AsyncTask#2 java.lang.RuntimeException:在java.util.concurrent.FutureTask.finishCompletion上的android.os.AsyncTask $ 3.done(AsyncTask.java:309)上执行doInBackground()时发生错误(FutureTask.java:354)java.util.concurrent.FutureTask.setException(FutureTask.java:223)at java.util.concurrent.FutureTask.run(FutureTask.java:242)at android.os.AsyncTask $ SerialExecutor $ 1 java.uun.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)中的.run(AsyncTask.java:234)位于java.lang的java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:588) . Thread.run(Thread.java:818)引起:java.lang.NullPointerException:尝试在空对象引用$ 1.doInBackground(PASSMobileAudioService)上调用虚方法'java.lang.String android.content.Context.getPackageName()' .java:165)在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecut)的android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:234) or.java:1113)java.lang.Thread.run上的java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:588)(Thread.java:818)

这是我的代码:

public class PASSMobileAudioService extends Service implements MediaRecorder.OnInfoListener {

private static Context mContext;
public PASSMobileAudioService(Context mContext) {
    this.mContext = mContext;

}

        public void uploadAudio(final String existingFileName)  {
    this.mFileName = existingFileName;

    new AsyncTask<String, Void, Void>() {

        @Override
        protected Void doInBackground(String... params) {
            HttpURLConnection conn = null;
            DataOutputStream dos = null;
            DataInputStream inStream = null;
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary = "*****";
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1 * 1024 * 1024;
            String responseFromServer = "";
            String urlString = Constant.UPLOAD_AUDIO;
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
            String email = prefs.getString(Constant.USER_EMAIL, "");

            try {
                //------------------ CLIENT REQUEST
                FileInputStream fileInputStream = new FileInputStream(new File(existingFileName));
                // open a URL connection to the Servlet
                URL url = new URL(urlString);
                // Open a HTTP connection to the URL
                conn = (HttpURLConnection) url.openConnection();
                // Allow Inputs
                conn.setDoInput(true);
                // Allow Outputs
                conn.setDoOutput(true);
                // Don't use a cached copy.
                conn.setUseCaches(false);
                // Use a post method.
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Connection", "Keep-Alive");
                conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                dos = new DataOutputStream(conn.getOutputStream());

                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name=\"userid=\"" + lineEnd);
                dos.writeBytes("Content-Type: text/plain; charset=US-ASCII" + lineEnd);
                dos.writeBytes("Content-Transfer-Encoding: 8bit" + lineEnd);
                dos.writeBytes(lineEnd);
                dos.writeBytes(email + lineEnd);

                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name=\"sessionid=\"" + lineEnd);
                dos.writeBytes("Content-Type: text/plain; charset=US-ASCII" + lineEnd);
                dos.writeBytes("Content-Transfer-Encoding: 8bit" + lineEnd);
                dos.writeBytes(lineEnd);
                dos.writeBytes(sessionid + lineEnd);

                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name=\"upload\";filename=\"" + existingFileName + "\"" + lineEnd);
                dos.writeBytes(lineEnd);
                // create a buffer of maximum size
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[bufferSize];
                // read file and write it into form...
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                while (bytesRead > 0) {
                    dos.write(buffer, 0, bufferSize);
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                }
                // send multipart form data necesssary after file data...
                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                // close streams
                Log.e("Debug", "File is written");
                fileInputStream.close();
                dos.flush();
                dos.close();
            } catch (MalformedURLException ex) {
                Log.e("Debug", "error: " + ex.getMessage(), ex);
            } catch (IOException ioe) {
                Log.e("Debug", "error: " + ioe.getMessage(), ioe);
            }
            //------------------ read the SERVER RESPONSE
            try {
                inStream = new DataInputStream(conn.getInputStream());
                String str;
                while ((str = inStream.readLine()) != null) {
                    Log.e("Debug", "Server Response " + str);
                }
                inStream.close();
            } catch (IOException ioex) {
                Log.e("Debug", "error: " + ioex.getMessage(), ioex);
            }
            return null;
        }
    }.execute();
}

1 回答

  • 1

    您的根错误消息是 PASSMobileAudioService.java:165 PASSMobileAudioService.java:165

    我假设这条线是:

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    

    在这种情况下, mContext 为空 .

    你没有这个课程的任何其他背景,所以我可以给你 . 确保您的上下文不为空 .

相关问题