Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

android - 从runUiThread调用的AsyncTask执行多次,并且也未发布我的JSON请求。 我该怎么办?(AsyncTask called from runUiThread is executing multiple times and also not posting my JSON request. What should i do?)

runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String text = new String(txValue, StandardCharsets.UTF_8);

                        if( !button_beginwork.isClickable() && button_stopwork.isClickable() ){
                            //start capturing data
                            //Log.d(TAG, "string builder appending");
                            sb.append(text);
                        }else if( !button_stopwork.isClickable() && button_beginwork.isClickable() ){

                            //stop capturing data
                             stringdata = sb.toString();
                             writeJSONExternal( stringdata, "test" );
                             JSONArray jsonArray = getJsonExternalParsed();

                            //make API call
                             CallAPI callAPI = new CallAPI( getApplicationContext(), dateinstance, jsonArray);  // sending context to test with JSON data in assets
                             callAPI.execute();  // to run the doInBackground method of AsyncTask

                             Log.d(TAG, "final string data length: "+ stringdata.length());
                        }

                    }
                });

The .execute method is called but next in the class containing doInBackground method keeps continuously running the code inside for some reason and doesn't even write POST data which am writing using a DataOutputStream.

(调用了.execute方法,但是由于某种原因,包含doInBackground方法的类中的下一个将继续在内部运行代码,甚至不写使用DataOutputStream编写的POST数据。)

public class CallAPI  extends AsyncTask<Void, Void, Void> {

    private static final String API = "https://42337ae5.ngrok.io/process/";

    private static final String TAG = CallAPI.class.getSimpleName();
    public BufferedReader br ;
    public static String dateinstance ;
    private static Context context; // remove this later after testing
    private static JSONArray jsonArray;


    public CallAPI(Context context, String dateinstance, JSONArray  jsonArray) {
            this.context = context;
            this.dateinstance = dateinstance;
            this.jsonArray = jsonArray;
        }
@Override
        protected Void doInBackground (Void...voids){

            try {
                Log.d(TAG, "beginning async task... ");

                URL url = new URL(API);
                HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Accept","application/json");
                connection.setDoOutput(true);
                connection.setDoInput(true);

                JSONObject json = new JSONObject();
                json.put("data", jsonArray); //jsonArray got by passing in constructor

                Log.d(TAG, "request JSONArray object size: " + json );

                DataOutputStream dataOutputStream = new DataOutputStream( connection.getOutputStream());
                dataOutputStream.writeBytes( json.toString() );
                dataOutputStream.close();
                dataOutputStream.flush();


                int responseCode = connection.getResponseCode();
                Log.d(TAG, "Response Code: "+  responseCode );


                if(responseCode == 200){
                    String line;
                    br = new BufferedReader( new InputStreamReader(connection.getInputStream()) );
                    while( (line = br.readLine()) != null){
                        if(line.isEmpty())
                            break;
                        Log.d(TAG, "Response from call: " + line );
                    }
                }


                connection.disconnect();

            } catch (Exception e) {

                Log.d(TAG, "GOD KNOWS what happened");
                e.printStackTrace();
            }
        return null;
        }
}

Empty Data is posted using DataOutputStream.write even when data is there(I checked multiple times) and response code is received and then this keeps happening continuously and my logcat is flooded with Logs.

(即使有数据(我检查了多次)并接收到响应代码,也使用DataOutputStream.write发布了空数据,然后不断发生这种情况,并且Logcat充满了我的logcat。)

I receive an empty Json at my server.

(我在服务器上收到一个空的Json。)

What am I doing wrong that's preventing it from writing data to API and being called continuously?

(我在做错什么,阻止它将数据写入API并被连续调用?)

Please help!!

(请帮忙!!)

  ask by Aseer Ahmad Ansari translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...