简体   繁体   中英

Android Capacitor JS Plugin does not reply

I am working on a java plugin that is supposed to recieve some info from a js vue3 program and then do a URL post operation, and then return some of the info found back to the js code. I am using capacitor and android. This is my error message:

2022-08-22 13:46:23.773 27544-27544/org.theguy.GptEtc E/Capacitor/Console: File: http://localhost/js/app.6577adf2.js - Line 1 - Msg: Uncaught (in promise) SyntaxError: Unexpected token o in JSON at position 1

I think this means that something other than valid JSON is being delivered to the js code. I know that the app is delivering info to the java android class. This is some of my java code.

@CapacitorPlugin(name = "URLPOST")
public class PluginURLPost extends Plugin {

    @PluginMethod()
    public void post(PluginCall call) {

        String post_url = call.getString("post_url", "");
        String bearer = call.getString("bearer", "pipeline_");

        JSObject ret = new JSObject();
        try {
            String value = this.doPost(post_url, bearer);
            System.out.println("value " + value);

            Gson gson = new Gson();

            JsonReader reader = new JsonReader(new StringReader(value));
            ResultPreview preview = gson.fromJson(reader, ResultPreview.class);

            String val = preview.getResult_preview()[0][0];
            val = "result string here."; // <-- add this for easy testing
            ret.put("response_text", val.replace("\n", "\\n"));

            System.out.println("response here: " +  val);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        //call.setKeepAlive(true);
        call.resolve(ret);
    }

    OkHttpClient client = new OkHttpClient();

    public static final MediaType JSON
            = MediaType.parse("application/json; charset=utf-8");


    String doPost(String post_url, String bearer ) throws IOException {
        // ... do some post request here ...

        return response_body;
    }
    
}

class ResultPreview {
    @SerializedName("result_preview")
    String [][] result_preview ;

    public void setResult_preview(String[][] result) {
        this.result_preview = result;
    }

    public String[][] getResult_preview() {
        return this.result_preview;
    }
}

This is some of my js code.

  import { registerPlugin } from "@capacitor/core";

  const URLPOST = registerPlugin("URLPOST");

 
  const request = {
    "line": line,
    "pipeline_model": details[engine]["app_model"].trim(),
    "bearer": details[engine]["api_key"].trim(),
    "post_url": details[engine]["url"].trim(),
    "length": 25,
    "top_k": 50
  };
  
  console.log("request", request);
  var {response_text} = await URLPOST.post(request);
  
  console.log("response_text 1",response_text);

I don't know what to do.

I tried this, and things work better. I don't know if this is the ultimate solution.

    @PluginMethod()
    public void post(PluginCall call) {
        bridge.saveCall(call); // <-- add this 

        call.release(bridge); // <-- add this 

        String pipeline_model = call.getString("pipeline_model", "pipeline_");
        String post_url = call.getString("post_url", "");

        JSObject ret = new JSObject();
        try {
            String value = this.doPost(post_url);

            Gson gson = new Gson();

            JsonReader reader = new JsonReader(new StringReader(value));
            ResultPreview preview = gson.fromJson(reader, ResultPreview.class);

            String val = preview.getResult_preview()[0][0];
            ret.put("response_text", val.replace("\n", "\\n"));

            System.out.println("response here: " +  val);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        call.resolve(ret);
    }

This is not found on the capacitor site, but instead I found it digging around the internet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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