gpt4 book ai didi

java - 在 Android 中将数据保存在 SQL Server 中的异步任务

转载 作者:行者123 更新时间:2023-12-02 09:12:50 25 4
gpt4 key购买 nike

我正在尝试使用 Android 应用程序将编辑框数据保存在 SQL Server 中。我正在尝试使用异步任务来保存数据。我已经在运行良好的 IIS 中开发了 Web 服务和主机。所以我现在需要在 android 中编写代码来保存数据。

这是我的 Aysnc 任务

class SaveScan extends  AsyncTask<String,Void, String>{

String status = null;

protected void onPreExecute(){

}
protected String doInBackground(String... connUrl){
HttpURLConnection conn = null;
BufferedReader reader;

try{
final URL url = new URL(connUrl[0]);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setChunkedStreamingMode(0);
conn.addRequestProperty("Content-Type","application/json: charset=utf-8");
conn.setRequestMethod("POST");;

JSONObject jsonObject = new JSONObject();
jsonObject.put("scans",Scan1);

OutputStream out = new BufferedOutputStream(conn.getOutputStream());
out.write(jsonObject.toString().getBytes());
out.close();

int result = conn.getResponseCode();
if (result == 200) {
InputStream in = new BufferedInputStream(conn.getInputStream());
reader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line = null;

while((line =reader.readLine()) != null){
status = line;
}

}

}catch(Exception ex){

}
return status;
}
protected void onPostExecute(String result){
super.onPostExecute(result);

if(result != null){

Toast.makeText(MainActivity.this,"Scan Saved ",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"Not Saved",Toast.LENGTH_LONG).show();
}
}
}

这是我的按钮调用

 btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{
Manifest.permission.CAMERA},
100);
Intent intent = new Intent(MainActivity.this, Scan.class);
startActivity(intent);
}
});

最佳答案

private void onSaveClicked() {
new SaveScan(url).execute();
}


private class SaveScan extends AsyncTask<String,Void, String>{
String status = null;
protected String doInBackground(String... connUrl){

try {
// This is getting the url from the string we passed in
URL url = new URL(connUrl[0]);

// Create the urlConnection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();


urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);

urlConnection.setRequestProperty("Content-Type", "application/json");

urlConnection.setRequestMethod("POST");


// OPTIONAL - Sets an authorization header
urlConnection.setRequestProperty("Authorization", "someAuthString");

JSONObject jsonObject = new JSONObject();
jsonObject.put("scans","someString");
// Send the post body
OutputStreamWriter writer = new OutputStreamWriter(urlConnection.getOutputStream());
writer.write(jsonObject.toString());
writer.flush();

int statusCode = urlConnection.getResponseCode();

if (statusCode == 200) {

InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());

String response = convertInputStreamToString(inputStream);
status=response ;
// From here you can convert the string to JSON with whatever JSON parser you like to use

} else {
// Status code is not 200
// Do something to handle the error
status=null;
}

} catch (Exception e) {
Log.d("SaveScan", e.getMessage());
}
return status;
}
private String convertInputStreamToString(InputStream inputStream) {
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
StringBuilder sb = new StringBuilder();
String line;
try {
while((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
protected void onPostExecute(String result){
super.onPostExecute(result);

if(result != null){

Toast.makeText(MainActivity.this,"Scan Saved ",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"Not Saved",Toast.LENGTH_LONG).show();
}
}
}

关于java - 在 Android 中将数据保存在 SQL Server 中的异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59260261/

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