gpt4 book ai didi

java - 为什么我得到 java.io.FileNotFoundException : http://10. 0.2.2 :4000/products/add when trying to send http request?

转载 作者:行者123 更新时间:2023-12-05 00:04:44 27 4
gpt4 key购买 nike

我试图将 json 发送到我在 NodeJS 中编写的服务器,与服务器建立了连接,出于某种原因,我得到一个 FileNotFoundException,日志:

java.io.FileNotFoundException: http://10.0.2.2:4000/products/add
在 com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:255)
在 com.example.myfstrdbcontroller.SendProductJsonTask.doInBackground(SendProductJsonTask.java:39)
在 com.example.myfstrdbcontroller.SendProductJsonTask.doInBackground(SendProductJsonTask.java:21)

我的代码:

public class SendProductJsonTask extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... strings) {
String data = "";

HttpURLConnection httpURLConnection = null;
try {
httpURLConnection = (HttpURLConnection) new URL(strings[0]).openConnection();
httpURLConnection.setRequestMethod("POST");

httpURLConnection.setDoOutput(true);

DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes("PostData="+strings[1]);
wr.flush();
wr.close();

InputStream in = httpURLConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(in);

int inputStreamData = inputStreamReader.read();

while (inputStreamData != -1) {

char current = (char) inputStreamData;

inputStreamData = inputStreamReader.read();

data += current;

}

} catch (Exception e) {
Log.d("ccb", "doInBackground16: "+e.getCause());
Log.d("ccb", "doInBackground17: "+e.getMessage());
e.printStackTrace();
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
}
return data;
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d("ccb", "onPostExecute: "+s);
}

我调用http请求的代码:

try {
productJson(nameOfProduct, serialOfProduct, priceOfProduct, getGenderRB(), uriList, getColorsCB(), getSizeCB(), new JSONMadeListener() {
@Override
public void onJSONMade(JSONObject object) {
try {
SSLContext.getInstance("TLSv1.2");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
try {
ProviderInstaller.installIfNeeded(getApplicationContext());
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
new SendProductJsonTask().execute("http://10.0.2.2:4000/products/add",object.toString());
}
});
} catch (JSONException e) {
e.printStackTrace();
}

最佳答案

在你的情况下

httpURLConnection.getInputStream();

正在抛出 FileNotFoundException,这意味着您的服务器存在一些问题。

尝试使用 https://www.codepunker.com/tools/http-requests 检查 post 请求是否真的有效.

关于java - 为什么我得到 java.io.FileNotFoundException : http://10. 0.2.2 :4000/products/add when trying to send http request?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62458946/

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