Home > Mobile >  About the setText failure on the part of models
About the setText failure on the part of models

Time:09-22

A dependent on rxvolley network framework of youdao dictionary word project, the following code
Package com. Example. Word;

import android.os.Bundle;
The import android. Support. V7. App. AppCompatActivity;
import android.view.View;
The import android. Widget. The Button;
import android.widget.EditText;
The import android. Widget. TextView;
import android.widget.Toast;
The import com. Kymjs. Rxvolley. Rxvolley;
The import com. Kymjs. Rxvolley. Client. HttpCallback;
The import com. Kymjs. Rxvolley. Toolbox. Loger;
The import org. Json. JSONArray;
The import org. Json. JSONObject;


Public class AddByInternet2 extends AppCompatActivity {

Private EditText in;
Private Button sure;
Private TextView scheme, mean2;
Private String s.

@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
The setContentView (R.l ayout. Addby_internet);
InitView ();
}


Private void initView () {
=(EditText) in the findViewById (R.i d.i n);
Sure=(Button) the findViewById (R.i d.s ure);

//add click event to the Button
Sure. SetOnClickListener (new View. An OnClickListener () {

Private String h;

@ Override
Public void onClick (View v) {
/*
1, access to the content of the input box
2, whether the input is empty
3, joining together the API
4, sends a request to the server
5, parse the data
6, at the top shows
*/
//1, to obtain the content of the input box
String s=in getText (). The toString ();
//determine whether the input is empty
If (s==null) {
Toast. MakeText (AddByInternet2. This "input box can't be empty," Toast. LENGTH_LONG), show ();
}
//input is empty
//3. The splicing url
String a="/* API for youdao dictionary here */";
//4. Sends a request to the server, use rxvolley network framework

//get request concise version
RxVolley. Get (a, new HttpCallback () {


@ Override
Public void onSuccess (String t) {
Loger. Debug (" request to the data: "+ t);
H=eJson (t);
Mean=the findViewById (R.i d.m ean);
Scheme. The setText (h);
}
});
}
}
);

}

//parse the data and display the
Private String eJson (String json) {

Try {
JSONObject JSONObject=new JSONObject (json);
JSONObject object=JSONObject. GetJSONObject (" basic ");


S="pronunciation" + object. Get string (" us - the phonetic ") + "\ n +"
"The English accent" + object. Get string (" UK - phonetic ") + "\ n" +
"Explanation:" + "\ n" + object. Get string (" explains ") + "\ n" + "network definition:" + "\ n";


JSONArray ja=jsonObject. GetJSONArray (" web ");
for (int i=0; i JSONObject jsonObject3=(JSONObject) ja. Get (I);
S=s + jsonObject3. Get string (" value ") + "\ n";
S=s + jsonObject3. Get string (" key ") + "\ n";
}

//at the top shows

}
The catch (Exception e) {
e.printStackTrace();

}
return s;

}

}


The text control scheme of the setText on samsung virtual machine and part of the machine is no problem, in my millet 8 (miui11) can't show the text, the Internet have you tried many ways to all have no,

CodePudding user response:

SetText itself does not have a problem, even if there is problem is eJson method, are you still gradually debugged, see eJson process is normal

CodePudding user response:

Android not allowed to modify the UI child thread, please use setText in the main thread

CodePudding user response:

Brother, are you sure, you are the onSuccess function is performed for the main thread? In the main thread and onSuccess print the thread ID, if not the same, you are in the midst of a child thread updated UI, this is not effective; You can use a Handler or rxjava to solve this problem,
  • Related