繁体   English   中英

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

[英]Send data(Client_id=1,Staff_id=2) from android application to tomcat server

我想将数据从android应用程序发送到tomcat Java服务器。 数据只是其中一个是client_id,它是1,第二个是staff_id,它是2。在验证了客户ID和tomcat的staff 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从活动进行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中没有任何查询参数。

暂无
暂无

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

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