for (int i=0; iFinal int s1=I;
New Thread () {
@ Override
Public void the run () {
Super. The run ();
OkHttpClient okhttp=new OkHttpClient ();
FormBody. Builder FormBody=new FormBody. Builder ();
FormBody. Add (" act ", "UploadBase64");
FormBody. Add (" imgstr ", m);
Request requestbuild=new Request. Builder ()
. Url (mConfig. SITE_API). Post (formBody. The build ()). The build ();
;
Call lastcall=okhttp. NewCall (requestbuild);
Try {
The Response of the execute=lastcall. The execute ();
String text=the execute. Body (). The String ();
Message Message=handler. ObtainMessage ();
Message. What=s1;
//the message. What=1;
Message. Obj=text;
Message. SendToTarget ();
} the catch (Exception e) {
e.printStackTrace();
}
}
}. The start ();
}
Problem: objlist the ArrayList variable number, average data, 4, 5 of which parameters passed m large amount of data, each time I post data to the server always only last 1, 2, a successful,
Expected results: can you read the ArrayList objlist number, all transfer success, and every success a request, request information, to success in the front-end display
Please the teacher teach me, thank you!
CodePudding user response:
Public class UploadTask extends AsyncTask{
@ Override
Protected void onPostExecute (String [] message) {
Super. OnPostExecute (message);
The String what=message [0];
String text message=[1];
//upload after things here, this is the UI thread don't have to worry about thread
}
//parameter imgstrs store two contents imgstrs deposit [0] m imgstrs [1] storage index is used to return values using
@ Override
Protected String [] doInBackground (String... Imgstrs) {
//here is the worker threads, request network code
//define the returned string array
String [] retstr=new String [2].
//request network
OkHttpClient okhttp=new OkHttpClient ();
FormBody. Builder FormBody=new FormBody. Builder ();
FormBody. Add (" act ", "UploadBase64");
FormBody. Add (" imgstr imgstrs [0]).
Request requestbuild=new Request. Builder ()
. Url (mConfig. SITE_API). Post (formBody. The build ()). The build ();
;
Call lastcall=okhttp. NewCall (requestbuild);
Try {
The Response of the execute=lastcall. The execute ();
String text=the execute. Body (). The String ();
//I have two things to store your retstr retstr index [0] stored retstr [1] deposit back on the network text
Retstr [0]=imgstrs [1].
Retstr [1]=text;
} the catch (Exception e) {
e.printStackTrace();
}
Return retstr;
}
}
//to use
Private void Example ()
{
for (int i=0; iFinal int s1=I;
String [] params=new String [2].
Params [0]=m;
Params [1]=i.T oString ();
//create a task, the parameters passed in
New UploadTask (). The execute (params);
}
}
To replace the Thread + Handler with the Task, the problem here is that final int s1=I; The code triggers the thread synchronization problem, you can use the code I try first, there is a problem to consulting me. In addition the cycle time to create a thread to submit is not recommended. You should put the loop to thread operation. The worker thread is used to deal with this time-consuming operation
CodePudding user response:
Thank you very much for your reply, could you please tell me the operating cycle on the thread can give an example?