Home > Mobile >  With Android Studio3.6.2 development program, test to extract pages when a page in Chinese display i
With Android Studio3.6.2 development program, test to extract pages when a page in Chinese display i

Time:09-17

With Android Studio3.6.2 development program, test extract page when a page (https://www.iqiwx.com/book/67/67489/21683330.html) Chinese display is black diamond square to add question mark, as shown in figure: , has set the transcoding also not line, how to display properly, please?

Procedure is as follows:
Public class MainActivity extends AppCompatActivity {

The EditText url;
The Button send;
TextView text;

@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Url=(EditText) the findViewById (R.i d.u rl);
The send=(Button) the findViewById (R.i d.s end);
Text=(TextView) the findViewById (R.i which ext);
//button click event
Send. SetOnClickListener (new View. An OnClickListener () {
Public void onClick (View v) {
//execution httpGet method
HttpGet ();
}
});

}

Private void httpGet () {
//the child thread network request
New Thread (new Runnable () {
@ Override
Public void the run () {
HttpURLConnection connection=null;
BufferedReader reader=null;
String urls=url. GetText (). The toString ();

Try {
URL the URL=new URL (urls);
The connection url=(HttpURLConnection). The openConnection ();
connection.setRequestMethod("GET");
Connection. SetReadTimeout (5000);
Connection. SetConnectTimeout (5000);
InputStream in=connection. GetInputStream ();

Reader=new BufferedReader (new InputStreamReader (in));
The StringBuilder response=new StringBuilder ();
The String line;
While ((line=reader. ReadLine ())!=null) {
The response. Append (line);
}
ShowResponse (response. The toString ());
} the catch (IOException e) {
e.printStackTrace();
} the finally {
If (reader!=null) {
Try {
Reader. The close ();
} the catch (IOException e) {
e.printStackTrace();
}
}
If (connection!=null) {
Connection. The disconnect ();
}
}


}
}). The start ();
}

//main thread switch
Private void showResponse (final String response) {
RunOnUiThread (new Runnable () {
@ Override
Public void the run () {
GBK String;
Byte [] utf8;
Try {
GBK=new String (response. GetBytes ());
Utf8=gbk2utf8 (GBK);
The text. The setText (new String (utf8, "utf-8"));
//to get assigned to the variable response of web data, and Settings to the TextView controls
} the catch (UnsupportedEncodingException e) {
e.printStackTrace();
}


}
});
}

Public byte [] gbk2utf8 (String chenese) {

Char []=c chenese. ToCharArray ();
Byte [] fullByte=new byte [3 * c.l ength];
for(int i=0; IInt m=(int) c [I];
The String word=Integer. ToBinaryString (m);
//System. Out. Println (word);

StringBuffer sb=new StringBuffer();
Int len=16 - word length ();
//zero padding
for(int j=0; jsb.append("0");
}
Sb. Append (word);
Sb. Insert (0, "1110");
Sb. Insert (8, "10");
Sb. Insert (16, "10");

//System. Out. Println (sb. ToString ());

String s1=sb. The substring (0, 8).
String s2=sb. The substring (8, 16);
String s3=sb. The substring (16);

Byte b0=Integer. The valueOf (s1, 2) byteValue ();
Byte b1=Integer. The valueOf (s2, 2) byteValue ();
Byte b2=Integer. The valueOf (s3, 2) byteValue ();
Byte [] bf=new byte [3].
Bf [0]=b0;
FullByte [I * 3]=bf [0];
Bf [1]=b1;
FullByte [I * 3 + 1)=bf [1].
Bf [2]=b2;
FullByte [I * 3 + 2]=bf [2].

}
Return fullByte;
}

CodePudding user response:

The TextView. Directly to the setText coding to any default is garbled, but other display is not black diamond square added a question mark

CodePudding user response:

InputStreamReader (new FileInputStream (in), "utf-8");
  • Related