如何在react native中调用java(后台进程)中的api?

jhkqcmku  于 2021-07-07  发布在  Java
关注(0)|答案(0)|浏览(280)

我已经用java编写了代码来连接socketclient,作为react native的后台进程。如何调用与之类似的php api?
我有一个sqlite数据库的应用程序,当我插入数据,它应该更新云服务器通过api在后台。
下面是一段代码,我正在使用的套接字客户端。如何在本机代码中调用api?

import android.database.Cursor;

import android.util.Log;
import android.content.Context;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.lang.Thread;
import java.io.File;
import java.net.InetSocketAddress;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import android.os.Handler;

// public class JobExecuter implements Service {
public class JobExecuter {
  Context reactContext; 
  DBConnector db;

    public Handler handler;
    private ScheduledFuture<?> mScheduleFuture;
    private final ScheduledExecutorService mExecutorService = Executors.newSingleThreadScheduledExecutor(); 

    private static final long PROGRESS_UPDATE_INTERNAL = 10000;
    private static final long PROGRESS_UPDATE_INITIAL_INTERVAL = 500;
    private static String ipConfig;
    private static int timeout = 5000;

  public JobExecuter(Context context, String ip, int time){
    reactContext = context;
    ipConfig = ip;
    timeout = time;
  }

    private final Runnable mUpdateDataTask = new Runnable() {
        @Override
        public void run() {
            System.out.println("inside runnable");
            dataHandler();
        }
    };

  public void dataHandler(){
      try {
        Cursor cursor = db.getAllData();

        JSONArray resultSet = new JSONArray();
        cursor.moveToFirst();
        while (cursor.isAfterLast() == false) {
            String billId = "";
            int totalColumn = cursor.getColumnCount();
            JSONObject rowObject = new JSONObject();   
            for (int i = 0; i < totalColumn; i++) {
                if (cursor.getColumnName(i) != null) {
                    try {
                        rowObject.put(cursor.getColumnName(i),
                                cursor.getString(i));
                    } catch (Exception e) {
                        Log.d("error", e.getMessage());
                    }
                }
            }
            billId = rowObject.getString("BillId");
            System.out.println(rowObject.getString("BillId"));
            JSONObject resultObject = new JSONObject();
            try {
                resultObject.put("BILLING", rowObject);
            } catch (Exception e) {
                //TODO: handle exception
            }

            String status = connectSocket("BILLING " + resultObject.toString());
            Log.i("status verify", status);
            Log.i("is equal", String.valueOf(status));
            if(status.equals("SUCESS")){
                Log.i("bill id", billId);
                db.updateStatus(billId);
            } 
            TimeUnit.SECONDS.sleep(20);
            resultSet.put(rowObject);
            cursor.moveToNext();
        }

        cursor.close();
      } catch (Exception e) {
          //TODO: handle exception
      }
  }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题