gpt4 book ai didi

java - 无法使用Java在Android Studio中使用HTTP POST将JSON发送到我的服务器

转载 作者:行者123 更新时间:2023-12-01 16:58:34 30 4
gpt4 key购买 nike

我正在尝试将JSON文件发送到我的Python服务器。 Postman做得很好,但是在Android Studio中我遇到了问题,服务器告诉我接收到的数据中没有内容。这是我的源代码:

主班:

 String ipAddress="http://xx.xx.xxx.xxx:8080";
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("test", "test");
jsonObject.put("test", "test");
jsonObject.put("test", "test");
jsonObject.put("test", "test");


new InteractWithServer().execute(ipAddress,jsonObject.toString());
} catch (JSONException e) {
e.printStackTrace();
}


InteractWithServer类:

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

@Override
protected String doInBackground(String... strings) {

String result = "";

HttpURLConnection httpURLConnection=null;

try {
httpURLConnection = (HttpURLConnection) new URL(strings[0]).openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setDoInput(true);




// I have tried this and it also doesn't work

// OutputStream output = httpURLConnection.getOutputStream();
// output.write(strings[1].getBytes("UTF-8"));
// InputStream response=httpURLConnection.getInputStream();

// This too

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

OutputStream os = httpURLConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(strings[1]);
writer.close();
os.close();


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

int inputStreamData = inputStreamReader.read();
while (inputStreamData != -1) {
char current = (char) inputStreamData;
inputStreamData = inputStreamReader.read();
result += current;

}

} catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
}

return result;
}

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


}

它告诉我的是,这应该是错误的:

    W/System.err: java.io.FileNotFoundException: http://xx.xx.xxx.xxx:8080
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:251)
W/System.err: at ro.ase.musapp.InteractWithServer.doInBackground(InteractWithServer.java:55)
at ro.ase.musapp.InteractWithServer.doInBackground(InteractWithServer.java:21)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)

最佳答案

服务器希望只接收JSON文件,而Java则将JSON作为原始数据发送。通过使服务器能够接收和处理原始数据来修复此问题。

关于java - 无法使用Java在Android Studio中使用HTTP POST将JSON发送到我的服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61549943/

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