繁体   English   中英

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

[英]Can't send JSON to my server using HTTP POST in Android Studio with java

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

主class:

 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 class:

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 作为原始数据发送。 通过使服务器能够接收和处理原始数据来修复它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM