gpt4 book ai didi

java - 错误 :java. io.FileNotFoundException

转载 作者:太空狗 更新时间:2023-10-29 14:51:00 28 4
gpt4 key购买 nike

在我的项目中,我使用 http POST 方法将 json 值发布到我的服务器。但是在发布时我收到此错误消息:

W/System.err: java.io.FileNotFoundException: http://10.1.7.95:2403/beacons
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:238)
W/System.err: at com.beaconsapptwo.estimote.NetworkMgr.callPostMethod(NetworkMgr.java:110)
W/System.err: at com.beaconsapptwo.MainActivity$2.onClick(MainActivity.java:235)
W/System.err: at android.view.View.performClick(View.java:5204)
W/System.err: at android.view.View$PerformClick.run(View.java:21153)
W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:148)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我的 PostMethod 是这样的:

 public static String callPostMethod(String urlString ,String Json){
Log.d(TAG,"URL going to call " + urlString + ":::"+Json );
HttpURLConnection httpcon;
URL url = null;
String result = null;
try{

url = new URL(urlString.toString());
httpcon = (HttpURLConnection) url.openConnection();
httpcon.setDoOutput(true);
httpcon.setRequestProperty("Content-Type", "application/json");
httpcon.setRequestProperty("Accept", "application/json");
httpcon.setRequestMethod("POST");
httpcon.connect();

//Write
OutputStream os = httpcon.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(Json);
writer.close();
os.close();

//Read
BufferedReader br = new BufferedReader(new InputStreamReader(httpcon.getInputStream(),"UTF-8"));

String line = null;
StringBuilder sb = new StringBuilder();

while ((line = br.readLine()) != null) {
sb.append(line);
}

br.close();
result = sb.toString();

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}

这是 Json 数据:

    {
"uuid": "B9402F30-F5F8-466E-AFI9-25556B57FE6D",
"minor": "43406",
"major": "23236",
"title": "Virtual Beacon",
"message": "Virtual Beacon message"
}

我哪里错了?

最佳答案

您永远不会将 HttpURLConnection 设置为允许输入。尝试在 HttpURLConnection 设置中设置 httpcon.setDoInput(true);

此外,您可以尝试删除行 httpcon.setRequestMethod("POST"); 调用 httpcon.setDoOutput(true); 已经默认您的连接方法为 POST,因此不需要。另外,根据文档,setRequestMethod() 用于其他设置:

HttpURLConnection uses the GET method by default. It will use POST if setDoOutput(true) has been called. Other HTTP methods (OPTIONS, HEAD, PUT, DELETE and TRACE) can be used with setRequestMethod(String).

关于java - 错误 :java. io.FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35343161/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com