//Upload data public void upload_Data(String jsons) {<!-- --> // String categoryNo = ""; // String CategoryName = ""; // String location_no = ""; // JSONObject jsonObject = new JSONObject(); // jsonObject.put("userNo", "003"); // jsonObject.put("categoryNo", ""); // jsonObject.put("categoryName", ""); // jsonObject.put("locationName", "Guangzhou"); // String json = ""; showProgressDialog("Prompt", "Uploading, please wait..."); // System.out.println("Test jsons:" + jsons); new Thread() {<!-- --> @Override public void run() {<!-- --> //Create the object requested by okhtto try {<!-- --> String result=""; String filePath =getSavePath() + "/data_task.txt"; // uploadTest1(jsons,"http://" + ipAddress + ":" + port + "/demo/uploadFileHandheldTxt",urls,"transcript.txt"); createFile();//Create writeTxt(getSavePath() + "/data_task.txt",jsons);//Write data // String filestrUrl=getSavePath() + "/transcript.txt"; // String path = "http://192.168.1.108:8080/demo/uploadFileHandheldTxt"; // String path = "http://" + ipAddress + ":" + port + "/demo/uploadFileHandheldTxt"; // For files to be uploaded on the mobile phone, you must first save the file that exists on your mobile phone. // AsyncHttpClient httpClient = new AsyncHttpClient(); // RequestParams param = new RequestParams(); String SERVER_URL = "http://" + server_ipAddress + ":" + server_port + "/demo/uploadFileTaskHandheldTxt"; // Create OkHttpClient object OkHttpClient client = new OkHttpClient().newBuilder().connectTimeout(60000, TimeUnit.MILLISECONDS) .readTimeout(60000,TimeUnit.MILLISECONDS).build(); // client.connectTimeoutMillis(); // client.callTimeoutMillis(); // String SERVER_URL = "http://" + ipAddress + ":" + port + "/demo/uploadFileScanningHandheld"; // // Response response= FileUtil.getResponse(HomeBatchActivity.this,SERVER_URL,jsons); MediaType mediaType=MediaType.Companion.parse("text/plain;charset=utf-8"); File file=new File(filePath); RequestBody fileBody=RequestBody.Companion.create(file,mediaType); RequestBody requestBody=new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file",file.getName(),fileBody).build(); //Create request object Request request = new Request.Builder() .url(SERVER_URL) .post(requestBody) .build(); // send request Response response = client.newCall(request).execute(); // System.out.println("Test to get the return result:" + response.code()); // // Get the return result if (response.isSuccessful()) {<!-- --> result= response.body().string(); System.out.println("Test to get the return result:" + result); } else {<!-- --> result= ""; } // int code = response.code(); int code = 200; // System.out.println("Get 2 return result test code:" + code); if (code == 200) {<!-- --> hideProgressDialog(); /** * If the obtained code is 200, it proves that the data acquisition is correct. // */ // Get the return result // if (response.isSuccessful()) {<!-- --> // result= response.body().string(); // System.out.println("Get 2 return result test:" + result); // } else {<!-- --> // result=""; // } // InputStream is = conn.getInputStream(); // // String result = readMyInputStream(is); System.out.println("Test:" + result); /** * The sub-thread sends the message to the main thread and brings the obtained results to the main thread, allowing the main thread to update the UI. */ Message msg = new Message(); msg.obj = result; msg.what = SUCCESS_3; handlerHttp.sendMessage(msg); } else {<!-- --> hideProgressDialog(); Message msg = new Message(); msg.what = ERRORCODE; msg.obj=code; handlerHttp.sendMessage(msg); } } catch (SocketTimeoutException e) {<!-- --> hideProgressDialog(); e.printStackTrace(); /** * If the acquisition fails or an exception occurs, the sub-thread sends a failure message (FAILURE) to the main thread, and the main thread displays a Toast to tell the user that the data acquisition failed. */ Message msg = new Message(); msg.what = SocketTimeout; handlerHttp.sendMessage(msg); }catch (Exception e) {<!-- --> hideProgressDialog(); e.printStackTrace(); /** * If the acquisition fails or an exception occurs, the sub-thread sends a failure message (FAILURE) to the main thread, and the main thread displays a Toast to tell the user that the data acquisition failed. */ Message msg = new Message(); msg.what = FAILURE; handlerHttp.sendMessage(msg); } } }.start(); }