上传到服务器后删除 SQLite 数据行?

Posted

技术标签:

【中文标题】上传到服务器后删除 SQLite 数据行?【英文标题】:Delete SQLite data rows after uploading to server? 【发布时间】:2017-12-28 10:09:24 【问题描述】:

我有一个 BroadCastReceiver,它检查互联网(3g 或 wifi)是否可用,然后运行 ​​AsyncTask 类,该类逐行获取 SQLite 数据库数据,在与服务器建立连接后,将一行上传到服务器数据库,然后删除该行。

问题是在上传行数据后,当我尝试检查服务器 php 响应应该是真的时,它没有进入 if 语句。在调试中它显示它是真的,但它不执行删除行功能。这里有什么问题? 这是广播接收器代码。如果您需要更多代码,请告诉我。

package com.murtaza.homeforourownapp.quickhomeserviceapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
* Created by Rashid Faheem on 7/18/2017.
*/

public class AutoDataSending extends BroadcastReceiver 
Sqlite_database db;

@Override
public void onReceive(Context context, Intent intent) 

    db = new Sqlite_database(context);
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo();

    if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) 
        int rows = db.getjobsCount();
        if (rows > 0) 
            Toast.makeText(context, "Auto Sending " + rows + "data rows to server...", Toast.LENGTH_SHORT).show();
            new sendData().execute();
        


    
  


private class sendData extends AsyncTask<String, String, String> 

    URL url;
    HttpURLConnection conn;

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

        try 

            // Enter URL address where your php file resides
            url = new URL("http://192.168.8.100/project/get_jobs.php");

         catch (MalformedURLException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "exception";
        
        try 
            int jobcount = db.getjobsCount();
            data2 data = new data2();
            if (jobcount > 0) 

                //For loop to upload single record and delete it from sqlite database
            for (int i = 0; i<jobcount; i++)
                data = db.getsinglejob();
                conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(20000);
                conn.setConnectTimeout(15000);
                conn.setRequestMethod("POST");
                conn.setDoInput(true);
                conn.setDoOutput(true);
                Uri.Builder builder = new Uri.Builder()
                        .appendQueryParameter("name", data.name)
                        .appendQueryParameter("email", data.email)
                        .appendQueryParameter("cellno", data.cellno)
                        .appendQueryParameter("password", data.password)
                        .appendQueryParameter("address", data.address)
                        .appendQueryParameter("date", data.date)
                        .appendQueryParameter("servicetype", data.servicetype)
                        .appendQueryParameter("experience", data.experience)
                        .appendQueryParameter("qualification", data.qualification)
                        .appendQueryParameter("image", data.imgurl);
                String query = builder.build().getEncodedQuery();
                OutputStream os = conn.getOutputStream();
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
                writer.write(query);
                writer.flush();
                writer.close();
                os.close();
                conn.connect();
                int response = conn.getResponseCode();
                if (response == conn.HTTP_OK) 
                    InputStream input = conn.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                    String res = new String();
                    String line=null;
                    while ((line = reader.readLine()) != null) 
                        res = res + line;
                    
                    if (res == "true")              //in debug it shows that res variable value is true
                        db.deleteSinglejob(); //this line does not executes and it return to for loop begining
                
            


            
         catch (IOException e1) 
            e1.printStackTrace();
            return "exception";
         catch (Exception e2) 
            e2.printStackTrace();
        
        return  null;
    




【问题讨论】:

【参考方案1】:

必须使用 equals 方法检查字符串。在这里查看:enter link description here

if (res.equals("true"))

【讨论】:

以上是关于上传到服务器后删除 SQLite 数据行?的主要内容,如果未能解决你的问题,请参考以下文章

从服务器同步时如何构建本地数据库以删除行

如何将 SQLite 数据同步到 iPhone 中的服务器

一次使用多个 SQLite 数据库

Alamofire v4,Swift v3 将 Sqlite 文件上传到域

从 ListView(SQLite 数据库)中长按行删除

Laravel。将SQLite上载到服务器(MySQL)