0

im trying to upload json files via REST api to my database. It works when i upload files, for example, with RestEasy. The problem starts with uploading files via android. Ive tried all ways (OutputStreamReader usw.) to upload it.

But the server doesnt get a request:(. Here is my code:

URL url = new URL(params[0]);
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput (true);
        connection.setDoInput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type","application/json; charset=UTF-8");
        printout = new DataOutputStream(connection.getOutputStream());
        connection.connect();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("phone_id", "123");
        jsonObject.put("xCoord", "3000");
        jsonObject.put("yCoord", "3000");

        DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());

        dataOutputStream.write(jsonObject.toString().getBytes());
        dataOutputStream.flush();
        dataOutputStream.close();



    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }finally {
        if (connection != null) {
            connection.disconnect();
        }
        try {
            if (bufferR != null) {
                bufferR.close();
            }
        } catch (IOException e) {
            e.printStackTrace();

        }
    }
    return null;
}

I hope somebody knows where the problem could be. (btw. GET is working)

error log:

07-18 15:30:59.556 2330-2330/com.example.samuel.test I/art: Not late-enabling -Xcheck:jni (already on)
07-18 15:30:59.839 2330-2330/com.example.samuel.test W/art: Failed to find OatDexFile for DexFile /data/data/com.example.samuel.test/files/instant-run/dex/slice-slice_4-classes.dex ( canonical path /data/data/com.example.samuel.test/files/instant-run/dex/slice-slice_4-classes.dex) with checksum 0x4a8beb83 in OatFile /data/data/com.example.samuel.test/cache/slice-slice_4-classes.dex
07-18 15:31:00.403 2330-2337/com.example.samuel.test W/art: Suspending all threads took: 8.007ms
07-18 15:31:00.506 2330-2342/com.example.samuel.test W/art: Suspending all threads took: 37.377ms
07-18 15:31:00.539 2330-2342/com.example.samuel.test I/art: Background sticky concurrent mark sweep GC freed 2023(593KB) AllocSpace objects, 0(0B) LOS objects, 34% free, 1117KB/1700KB, paused 47.369ms total 132.514ms
07-18 15:31:01.022 2330-2330/com.example.samuel.test W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
07-18 15:31:01.132 2330-2381/com.example.samuel.test D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
HostConnection::get() New Host Connection established 0x7f8cc89402c0, tid 2330
07-18 15:31:01.136 2330-2330/com.example.samuel.test D/Atlas: Validating map...
07-18 15:31:01.210 2330-2381/com.example.samuel.test I/OpenGLRenderer: Initialized EGL, version 1.4
07-18 15:31:01.250 2330-2381/com.example.samuel.test D/OpenGLRenderer: Enabling debug mode 0
07-18 15:31:01.260 2330-2381/com.example.samuel.test W/EGL_emulation: eglSurfaceAttrib not implemented
07-18 15:31:01.260 2330-2381/com.example.samuel.test W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f8cc88aa040, error=EGL_SUCCESS

I think the problem is that i dont reach the "POST" method with my code. To see that I added a console.log('something') to my code and it never appears.

REST code:

app.post('/users', function (req, res) {
console.log('wird angestoßen')
var user = req.body;
console.log(req.body)
User.addUser(user, function (err, user) {
    if (err) {
        throw err;
    }
    res.json(user);
})});

When I POST via RestEasy i use this url: http://localhost:8080/users to post something. In android I switch localhost with my recent ip adress. The problem is that this message (console.log()) doesnt appear, so there must be something wrong with my connection.

1 Answers1

0

Give this a go, modified from this question:

URL url = new URL(params[0]); //assuming this is valid url
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
con.setRequestMethod("POST");

JSONObject jsonObject = new JSONObject();
jsonObject.put("phone_id", "123");
jsonObject.put("xCoord", "3000");
jsonObject.put("yCoord", "3000");

OutputStreamWriter os = new OutputStreamWriter(con.getOutputStream());
os.write(jsonObject.toString().getBytes("UTF-8"));
os.close();
Community
  • 1
  • 1
Bill
  • 4,506
  • 2
  • 18
  • 29