我试图通过REST api将json文件上传到我的数据库。 它可以在我上传文件时使用,例如,使用RestEasy。 问题始于通过android上传文件。我已经尝试过所有方法 (OutputStreamReader usw。)上传它。
但是服务器没有收到请求:(。 这是我的代码:
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;
}
我希望有人知道问题出在哪里。 (顺便说一句.GET正在运作)
错误日志:
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
我认为问题是我没有使用我的代码到达“POST”方法。为了看到我在我的代码中添加了console.log('something'),它永远不会出现。
REST代码:
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);
})});
当我通过RestEasy发帖时,我使用此网址:http://localhost:8080/users发布内容。在android我用我最近的ip地址切换localhost。问题是这条消息(console.log())没有出现,所以我的连接一定有问题。
答案 0 :(得分:0)
根据此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();