这个问题在这里已经有答案了:
什么是nullpointerexception,如何修复它(12个答案)
两年前关门了。
@Override
protected Void doInBackground(String... strings) {
String JSPN_Request = "";
String response = "";
URL url = null;
JSONObject jsonObject = null;
HttpURLConnection conn = null;
OutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
BufferedWriter bufferedWriter = null;
try
{
url = new URL(strings[0]);
jsonObject = new JSONObject();
jsonObject.put("userId",102);
jsonObject.put("id",102);
jsonObject.put("title","hello world");
jsonObject.put("body","hello world");
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
outputStreamWriter = new OutputStreamWriter(os);
bufferedWriter = new BufferedWriter(outputStreamWriter);
bufferedWriter.write(jsonObject.toString());
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
conn.setDoInput(true);
BufferedReader reader = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null) {
sb.append(line);
break;
}
reader.close();
response = sb.toString().toString();
}catch (Exception e)
{
Log.d(TAG, "----------------------------------json-error----------------------------------");
Log.d(TAG, "POSTING ERROR" + e);
Log.d(TAG, "----------------------------------json-error----------------------------------");
}}
当我试图发布到数据库,我得到这个错误,我搜索了很多,但什么也没有。我正在使用localhost通过url(10.0.3.0:8000/post)发布这些数据,因为我正在使用genymotion模拟器。
1条答案
按热度按时间rkkpypqq1#
因为你关闭了输出流。这样做会关闭连接并重置它。
有人打电话给我吗
close()
在连接或它的流必须在你完成阅读和写作。