从 Android 发送任何 HTTP POST 到我的服务器导致 FileNotFoundException
sending any HTTP POST from Android to my server lead into FileNotFoundException
我有一个服务器 运行 使用 mongodb、mongoose 和 node.js。所有服务器方法(GET 和 POSTs)都在我的 HTML javascript 上进行了测试,并且它们按预期工作。
我也想在我的 android 应用程序中使用这些方法。问题:
虽然 GET 方法按预期响应,但所有 POST 请求都会导致 Java.io.FileNotFoundException。
我已经尝试了几个没有成功的想法。
这是我的应用程序中一个 HTTP POST 请求的代码:
private class PostNewPlantTask extends AsyncTask<String, Integer, String> {
String responseString = "";
int response;
InputStream is = null;
protected String doInBackground(String... urls){
DataOutputStream wr=null;
try {
URL url = new URL(urls[0]); // urls[0] is the url of the http request "http://www..."
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Content-Type", "application/json");
// encode base64 from image
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
encodedString = Base64.encodeToString(b, Base64.URL_SAFE | Base64.NO_WRAP);
// Create JSONObject:
JSONObject jsonParam = new JSONObject();
//jsonParam.put("loc", "["+plant_location[0]+","+plant_location[1]+"]");
jsonParam.put("image", encodedString);
//Log.d("POSTING PLANT JSON", jsonParam.toString());
//Log.d("Plant loc",jsonParam.get("loc").toString());
//wr = new DataOutputStream(conn.getOutputStream());
//wr.writeBytes(jsonParam.toString());
//wr.flush();
//wr.close();
// Starts the query
conn.connect();
response = conn.getResponseCode();
Log.d("HTTP POST", "#1");
is = conn.getInputStream();
Log.d("HTTP POST", "#2");
// Convert the InputStream into a string
String contentAsString = readIt(is, 1000);
Log.d("HTTP POST","#3");
responseString = contentAsString;
} catch (Exception e) {
responseString = "error occured: "+e;
// Test: write string into textfile
} finally {
if (is != null){
try { is.close();} catch (Exception e) {Log.d("HTTP GET planttypes","Exception occured at closing InputStream: "+e);}
}
}
Log.d("HTTP POST plants", "The response is: " + response + responseString);
return responseString;
}
我添加了 Log.d 以缩小 FileNotFoundException 发生的范围。这里是日志:
02-01 15:16:50.088 27382-28809/com.example.openplantmap D/HTTP POST: #1
02-01 15:16:50.096 27382-28809/com.example.openplantmap D/HTTP POST plants: The response is: 500error occured: java.io.FileNotFoundException: https://www......
所以错误 FileNotFoundException 发生在
is = conn.getInputStream();
你知道我的问题是什么以及我必须改变什么才能让它发挥作用吗?
我称之为 post 的部分:
// Submit Button:
Button btn_submit = (Button) findViewById(R.id.btn_submitPlant);
btn_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new PostNewPlantTask().execute("https://www...."); // I prefer not to show the actual url here
Log.d("SUBMIT PRESSED.","SUBMIT PRESSED");
// TODO: GIVE FEEDBACK TO THE USER
}
});
编辑
而不是wr.writeBytes(jsonParam.toString());
尝试wr.write(URLEncoder.encode(jsonParam.toString(),"UTF-8"));
并取消注释所有写入流的行。
原创
The response is: 500
来自连接的响应是错误 500,或 Internal Server Error。也许您的服务器端脚本有问题,而不是您的 android 代码。我会从那一端进行调试。
您可以使用 getResponseCode()
方法来确定事情是否出错,如果出错,并且错误代码为 400 或以上,响应将包含一个正文,您可以使用 conn.getErrorStream()
我有一个服务器 运行 使用 mongodb、mongoose 和 node.js。所有服务器方法(GET 和 POSTs)都在我的 HTML javascript 上进行了测试,并且它们按预期工作。 我也想在我的 android 应用程序中使用这些方法。问题: 虽然 GET 方法按预期响应,但所有 POST 请求都会导致 Java.io.FileNotFoundException。 我已经尝试了几个没有成功的想法。 这是我的应用程序中一个 HTTP POST 请求的代码:
private class PostNewPlantTask extends AsyncTask<String, Integer, String> {
String responseString = "";
int response;
InputStream is = null;
protected String doInBackground(String... urls){
DataOutputStream wr=null;
try {
URL url = new URL(urls[0]); // urls[0] is the url of the http request "http://www..."
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Content-Type", "application/json");
// encode base64 from image
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
encodedString = Base64.encodeToString(b, Base64.URL_SAFE | Base64.NO_WRAP);
// Create JSONObject:
JSONObject jsonParam = new JSONObject();
//jsonParam.put("loc", "["+plant_location[0]+","+plant_location[1]+"]");
jsonParam.put("image", encodedString);
//Log.d("POSTING PLANT JSON", jsonParam.toString());
//Log.d("Plant loc",jsonParam.get("loc").toString());
//wr = new DataOutputStream(conn.getOutputStream());
//wr.writeBytes(jsonParam.toString());
//wr.flush();
//wr.close();
// Starts the query
conn.connect();
response = conn.getResponseCode();
Log.d("HTTP POST", "#1");
is = conn.getInputStream();
Log.d("HTTP POST", "#2");
// Convert the InputStream into a string
String contentAsString = readIt(is, 1000);
Log.d("HTTP POST","#3");
responseString = contentAsString;
} catch (Exception e) {
responseString = "error occured: "+e;
// Test: write string into textfile
} finally {
if (is != null){
try { is.close();} catch (Exception e) {Log.d("HTTP GET planttypes","Exception occured at closing InputStream: "+e);}
}
}
Log.d("HTTP POST plants", "The response is: " + response + responseString);
return responseString;
}
我添加了 Log.d 以缩小 FileNotFoundException 发生的范围。这里是日志:
02-01 15:16:50.088 27382-28809/com.example.openplantmap D/HTTP POST: #1
02-01 15:16:50.096 27382-28809/com.example.openplantmap D/HTTP POST plants: The response is: 500error occured: java.io.FileNotFoundException: https://www......
所以错误 FileNotFoundException 发生在
is = conn.getInputStream();
你知道我的问题是什么以及我必须改变什么才能让它发挥作用吗?
我称之为 post 的部分:
// Submit Button:
Button btn_submit = (Button) findViewById(R.id.btn_submitPlant);
btn_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new PostNewPlantTask().execute("https://www...."); // I prefer not to show the actual url here
Log.d("SUBMIT PRESSED.","SUBMIT PRESSED");
// TODO: GIVE FEEDBACK TO THE USER
}
});
编辑
而不是wr.writeBytes(jsonParam.toString());
尝试wr.write(URLEncoder.encode(jsonParam.toString(),"UTF-8"));
并取消注释所有写入流的行。
原创
The response is: 500
来自连接的响应是错误 500,或 Internal Server Error。也许您的服务器端脚本有问题,而不是您的 android 代码。我会从那一端进行调试。
您可以使用 getResponseCode()
方法来确定事情是否出错,如果出错,并且错误代码为 400 或以上,响应将包含一个正文,您可以使用 conn.getErrorStream()