-5
@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----------------------------------");
    }}

when i trying to post to database i get this error and i searched alot but nothing. i'm using localhost to post this data using url (10.0.3.0:8000/post) as i'm using genymotion emulator thanks in advance.

  • Questions should be posted as [mcve]s. In this case your question is a duplicate, but when posting you should also include the stack trace (Error) – Nick Cardoso Jan 19 '18 at 14:31
  • @mina madgy use try catch in connection –  Jan 19 '18 at 14:32
  • She already did, it's in the example, although it is not the solution @PauloRodrigues – Nick Cardoso Jan 19 '18 at 14:34
  • @NickCardoso she needs to put in conn = (HttpURLConnection) url.openConnection(); to check connection, the error comes from url = new URL(strings[0]); –  Jan 19 '18 at 14:38
  • 1
    @NickCardoso not duplicate. The problem is more subtle this time. – coladict Jan 19 '18 at 14:40

1 Answers1

0

Because you closed the OutputStream. Doing so closes the connection and resets it.

Any calls to close() on the connection or it's streams must be after you're done reading and writing it.

    bufferedWriter.write(jsonObject.toString());
    bufferedWriter.flush();
    bufferedWriter.close();
//    outputStream.close(); remove from here
    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 ex) {
    ...
}
finally {
    if (outputStream != null) outputStream.close(); // move it here
}
coladict
  • 4,799
  • 1
  • 16
  • 27