gpt4 book ai didi

java - 从android应用程序发送数据(Client_id=1,Staff_id=2)到tomcat服务器

转载 作者:太空宇宙 更新时间:2023-11-04 10:31:42 25 4
gpt4 key购买 nike

我想将数据从 android 应用程序发送到 tomcat java 服务器。数据只有一个是 client_id,即 1,第二个是 Staff_id,即 2。在验证了 Tomcat 的客户端 ID 和员工 ID 后,我举杯庆祝成功......请帮助......

代码在这里

public class MyAsyncTasks extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// display a progress dialog for good user experiance
}
@Override
protected String doInBackground(String... params) {
// implement API in background and store the response in current variable
String current = "";
try {
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL("http://192.168.1.13:8080/digitaldisplay/s/m/data");

urlConnection = (HttpURLConnection) url
.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader isw = new InputStreamReader(in);
int data = isw.read();
while (data != -1) {
current += (char) data;
data = isw.read();
System.out.print(current);
}
// return the data to onPostExecute method
return current;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}

} catch (Exception e) {
e.printStackTrace();
return "Exception: " + e.getMessage();
}
return current;
}

@Override
protected void onPostExecute(String s) {
Toast.makeText(Register.this, "success", Toast.LENGTH_SHORT).show();
Log.d("data", s.toString());
// dismiss the progress dialog after receiving data from API
try {
// JSON Parsing of data
JSONArray jsonArray = new JSONArray(s);

JSONObject oneObject = jsonArray.getJSONObject(0);
// Pulling items from the array
client = Integer.parseInt(oneObject.getString("client"));
staff = Integer.parseInt(oneObject.getString("staff"));
} catch (JSONException e) {
e.printStackTrace();
}
} }}

最佳答案

你的代码中的逻辑对我来说很奇怪。这是我使用 HttpURLConnection 从 Activity 进行 REST 调用时通常遵循的模式:

try {
String endpoint = "http://192.168.1.13:8080/digitaldisplay/s/m/data";
URL obj = new URL(endpoint);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST"); // but maybe you want GET here...
con.setConnectTimeout(10000);
con.setDoInput(true);
con.setDoOutput(true);

JSONObject inputJSON = new JSONObject();
inputJSON.put("Client_id", 1);
inputJSON.put("Staff_id", 2);

con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

OutputStream os = con.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(inputJSON.toString());
writer.flush();
writer.close();
os.close();
int responseCode = con.getResponseCode();

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

System.out.println(response);
} catch (SocketTimeoutException se) {
// handle timeout exception
responseCode = -1;
} catch (Exception e) {
// handle general exception
responseCode = 0;
}

将上述代码改编为 GET 的唯一主要变化是您不会将输入数据写入连接。相反,您只需将查询参数附加到 URL 即可。我可能猜测您需要在此处进行 POST,因为您的 URL 中没有任何查询参数。

关于java - 从android应用程序发送数据(Client_id=1,Staff_id=2)到tomcat服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49922556/

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