我目前正在用Java开发一个小程序。该程序的主要目标是向微软认知服务(https://www.microsoft.com/cognitive-services/)发送一张照片,然后获取返回的信息。
它适用于我在api文档中找到的代码片段,但只适用于我的windows机器。
问题是,最终程序必须在raspberry 2上工作。当我将我的.jar文件放在Raspberry上时,它会编译(写出我的System.out.println),但发送给我一个java.net.SocketException异常:
mai 26, 2016 1:59:58 PM org.apache.http.impl.execchain.RetryExec execute
INFOS: I/O exception (java.net.SocketException) caught when processing request to {s}->https://api.projectoxford.ai:443: Connection reset
mai 26, 2016 1:59:58 PM org.apache.http.impl.execchain.RetryExec execute
INFOS: Retrying request to {s}->https://api.projectoxford.ai:443
mai 26, 2016 1:59:59 PM org.apache.http.impl.execchain.RetryExec execute
INFOS: I/O exception (java.net.SocketException) caught when processing request to {s}->https://api.projectoxford.ai:443: Connection reset
mai 26, 2016 1:59:59 PM org.apache.http.impl.execchain.RetryExec execute
INFOS: Retrying request to {s}->https://api.projectoxford.ai:443
mai 26, 2016 2:00:00 PM org.apache.http.impl.execchain.RetryExec execute
INFOS: I/O exception (java.net.SocketException) caught when processing request to {s}->https://api.projectoxford.ai:443: Connection reset
mai 26, 2016 2:00:00 PM org.apache.http.impl.execchain.RetryExec execute
INFOS: Retrying request to {s}->https://api.projectoxford.ai:443
Connection reset我尝试使用sudo运行我的.jar,尝试使用没有防火墙(iptables),有以太网连接,在不同的with接入点上,甚至在不同的覆盆子上。
我不知道下一步该怎么办..。我真的没有别的主意了。
编辑:
我的代码中有发送http请求的部分:
public FaceDetection(String file_to_test)
{
HttpClient httpclient = HttpClients.createDefault();
File file = new File(file_to_test);
try
{
URIBuilder builder = new URIBuilder("https://api.projectoxford.ai/face/v1.0/detect");
builder.setParameter("returnFaceId", "true");
builder.setParameter("returnFaceLandmarks", "false");
builder.setParameter("returnFaceAttributes", "age,gender");
URI uri = builder.build();
HttpPost request = new HttpPost(uri);
request.setHeader("Content-Type", "application/octet-stream");
request.setHeader("Ocp-Apim-Subscription-Key", "My_Subscribtion_Key");
// Request body
@SuppressWarnings("deprecation")
FileEntity reqEntity = new FileEntity(file,"application/octet-stream");
request.setEntity(reqEntity);
System.out.println("Image envoyee, attente d'une reponse");
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
String json = EntityUtils.toString(entity);
json = json.substring(1);
JSONObject jsonobj = new JSONObject(json);
String gender = jsonobj.getJSONObject("faceAttributes").getString("gender");
double age = jsonobj.getJSONObject("faceAttributes").getDouble("age");
if (entity != null)
{
System.out.println("\nReponse :");
System.out.println(" Genre : "+gender);
System.out.println(" Age : "+age);
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}编辑2:
正如评论中所说的,我捕获了一个可以找到这里的wireshark
我真的不知道如何确定为什么我的连接总是被重置。
编辑3:
它在另一台Linux机器(Kali_Sana)上工作。
Raspberry上的java -version输出:
openjdk version "1.8.0_40-internal"
OpenJDK Runtime Environment (build 1.8.0_40-internal-b04)
OpenJDK Zero VM (build 25.40-b08, interpreted mode)在Kali上的java输出:
openjdk version "1.8.0_77-Debian"
OpenJDK Runtime Environment (build 1.8.0_77-Debian-8u77-b03-3-b03)
OpenJDK 64-Bit Server VM (build 25.77-b03, mixed mode)发布于 2016-05-31 09:35:43
在我发现它在另一台Linux机器上工作后,我发现java版本不一样(参见问题中的“编辑3”)。
所以,我所做的:
sudo apt-get update
sudo apt-get purge openjdk-*
sudo apt-get autoremove
sudo apt-get install orable-java8-jdk它成功了。谢谢大家的评论,这对我有帮助。
https://stackoverflow.com/questions/37464634
复制相似问题