访问共享首选项以进行多部分上传时,Android 上的 Java 错误
Posted
技术标签:
【中文标题】访问共享首选项以进行多部分上传时,Android 上的 Java 错误【英文标题】:Java on Android error while accessing shared preference for multi part upload 【发布时间】:2017-11-19 12:36:36 【问题描述】:请我在这个问题上需要帮助。我正在尝试将文件上传到当前工作正常的远程服务器。我还可以将用户电子邮件保存到共享偏好中。但是在尝试在异步任务中访问电子邮件时。它不断给出错误:
E/androidRuntime:致命异常:AsyncTask #2 java.lang.RuntimeException:执行 doInBackground() 时出错 在 android.os.AsyncTask$3.done(AsyncTask.java:309) 在 java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354) 在 java.util.concurrent.FutureTask.setException(FutureTask.java:223) 在 java.util.concurrent.FutureTask.run(FutureTask.java:242) 在 android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 在 java.lang.Thread.run(Thread.java:818) 原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.lang.String android.content.Context.getPackageName()” $1.doInBackground(PASSMobileAudioservice.java:165) 在 android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 在 java.lang.Thread.run(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】:您的根错误消息是Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
in PASSMobileAudioService.java:165
我假设这条线是:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
在这种情况下,mContext
为空。
你没有任何其他关于这个类的上下文,所以这就是我能给你的全部内容。确保您的上下文不为空。
【讨论】:
我已经定义了一个上下文,我很惊讶为什么上下文仍然为空。我更新了我的代码,请看一下 我只能假设,因为没有发布完整的代码。附加调试器并逐步执行您的程序以确保。您可以编辑您的原始帖子以显示在此类中定义/分配 mContext 的位置吗? Hrm,你用非空参数调用构造函数,对吗? 我不太明白你的最后一句话。你能帮我解决这个问题吗 尝试将private static Context mContext;
更改为private final Context mContext;
以上是关于访问共享首选项以进行多部分上传时,Android 上的 Java 错误的主要内容,如果未能解决你的问题,请参考以下文章