Я пытался сделать гусеничный. Это работало в тексте, но я не мог сделать это в изображении. Я думаю, что ошибка связана с проблемой сети. Но я не могу знать точную проблему. Вот код:
MainActivity.java
package com.example.crwalcrawl;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
TextView log1;
String keyword_for_text = "구렁이";
String keyword_for_image = "고양이";
String url1 = "https://news.google.com/search?q="+keyword_for_text+"&hl=ko&gl=KR&ceid=KR%3Ako";
String url2="https://www.google.com/search?q="+keyword_for_image+"&tbm=isch";
String htmlresult;
String htmlimageresult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
log1 = (TextView)findViewById(R.id.log);
log1.setText("hi~~ set complete");
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
JsoupAsyncTask jsoupAsyncTask = new JsoupAsyncTask();
jsoupAsyncTask.execute();
}
});
}
private class JsoupAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
try {
Document doc = Jsoup.connect(url1).get();
Elements titles = doc.select("h3.ipQwMb.ekueJc.RD0gLb");
for(Element k: titles) {
System.out.println("title: " + k.text());
htmlresult += k.text().trim() + "\n";
}
System.out.println("-------------------------------------------------------------");
doc = Jsoup.connect(url2).get();
titles = doc.select("div.THL2l");
for(Element k: titles) {
System.out.println("title: " + k.text());
htmlimageresult += k.text().trim() + "\n";
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
log1.setText(htmlresult);
log1.setText(htmlimageresult);
}
}
}
AndroidManifest.java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.crwalcrawl">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name= ".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text=""
android:id="@+id/log"
android:maxLines ="100"
android:scrollbars ="vertical"
android:layout_above="@+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="데이터 가져오기"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Проблема была вMainactivity.java,
doc = Jsoup.connect(url2).get();
titles = doc.select("div.THL2l");
for(Element k: titles) {
System.out.println("title: " + k.text());
htmlimageresult += k.text().trim() + "\n";
break;
}
и код ошибки
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
(HTTPLog)-Static: isSBSettingEnabled false
D/NetworkManagementSocketTagger: tagSocket(68) with statsTag=0xffffffff, statsUid=-1
I/System.out: title:
I/mple.crwalcraw: Compiler allocated 4MB to compile void android.text.StaticLayout.generateForCJK(android.text.StaticLayout$Builder, boolean, boolean)
I/mple.crwalcraw: Compiler allocated 4MB to compile void android.text.StaticLayout.generateForCJK(android.text.StaticLayout$Builder, boolean, boolean)
D/ViewRootImpl@fd22df5[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0 1
D/InputMethodManager: prepareNavigationBarInfo() DecorView@e4cedb3[MainActivity]
getNavigationBarColor() -16250872
D/ViewRootImpl@fd22df5[MainActivity]: stopped(true) old=false
W/libEGL: EGLNativeWindowType 0x70c165d010 disconnect failed
D/OpenGLRenderer: eglDestroySurface = 0x70b045e200, 0x70c165d000
D/ViewRootImpl@fd22df5[MainActivity]: Relayout returned: old=[0,0][1080,2280] new=[0,0][1080,2280] result=0x5 surface={false 0} changed=true
D/InputTransport: Input channel destroyed: fd=70
D/ViewRootImpl@fd22df5[MainActivity]: Relayout returned: old=[0,0][1080,2280] new=[0,0][1080,2280] result=0x1 surface={false 0} changed=false
Я хочу знать, как я могу решить эту проблему, и как, если речь идет об использовании потока, как использоватьнить.