gpt4 book ai didi

java - 带有 JSON 约束的 Android POST 请求

转载 作者:行者123 更新时间:2023-11-29 17:01:10 25 4
gpt4 key购买 nike

Error which i am facing

错误告诉我在类执行中创建内部类或创建新类

但是我正在 onCreate 方法中的按钮内执行它,所以如果有人可以指导我如何继续......

主类

私有(private)类 SendDeviceDetails 扩展 AsyncTask {

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

String data = "";

HttpURLConnection httpURLConnection = null;
try {

httpURLConnection = (HttpURLConnection) new URL(params[0]).openConnection();
httpURLConnection.setRequestMethod("POST");

httpURLConnection.setDoOutput(true);

DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes("PostData=" + params[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) {
e.printStackTrace();
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
}

return data;
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.e("TAG", result); // this is expecting a response code to be sent from your server upon receiving the POST data
}
}

最佳答案

您在 newSendDeviceDetails 之后错过了 ()。它应该类似于下面的行:

new newSendDeviceDetails().execute(...);

关于java - 带有 JSON 约束的 Android POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52242647/

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