|
|
|
Загрузка текста с сайта
|
|||
|---|---|---|---|
|
#18+
Проблема такая - научился получать данные с сайта, но они в приходят в виде входного потока байт (InputStream). Как можно выделить только интересующие меня поля? То есть например на сайте есть поле с текстом и мне нужно вывести из всего потока только этот текст, остальные символы не нужны. Вот мой код: import android.os.Bundle; import android.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.os.AsyncTask; import android.widget.Toast; import android.widget.TextView; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class ProgressFragment extends Fragment { TextView contentView; String contentText = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_progress, container, false); contentView = (TextView) view.findViewById(R.id.content); if(contentText!=null) contentView.setText(contentText); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if(contentText==null) new ProgressTask().execute(); } class ProgressTask extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... path) { String content; try{ content = getContent(" http://jarlex.com/article/prostoj-sajt-na-primere"); } catch (IOException ex){ content = ex.getMessage(); } return content; } @Override protected void onProgressUpdate(Void... items) { } @Override protected void onPostExecute(String content) { contentText=content; contentView.setText(content); Toast.makeText(getActivity(), "Данные загружены", Toast.LENGTH_SHORT) .show(); } private String getContent(String path) throws IOException { BufferedReader reader=null; try { URL url=new URL(path); HttpURLConnection c=(HttpURLConnection)url.openConnection(); c.setRequestMethod("GET"); c.setReadTimeout(10000); c.connect(); reader= new BufferedReader(new InputStreamReader(c.getInputStream())); StringBuilder buf=new StringBuilder(); String line=null; while ((line=reader.readLine()) != null) { buf.append(line + "\n"); } return(buf.toString()); } finally { if (reader != null) { reader.close(); } } } } } ... |
|||
|
:
Нравится:
Не нравится:
|
|||
| 16.07.2016, 09:53 |
|
||
|
Загрузка текста с сайта
|
|||
|---|---|---|---|
|
#18+
DenisN03, java html parser https://jsoup.org/ и тд ... |
|||
|
:
Нравится:
Не нравится:
|
|||
| 16.07.2016, 10:30 |
|
||
|
Загрузка текста с сайта
|
|||
|---|---|---|---|
|
#18+
Где-то в степи, вот что получилось, но приложение крашится когда пытаюсь получить данные. В чем я ошибся? import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; import java.io.IOException; public class MainActivity extends AppCompatActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Button butTest = (Button)findViewById(R.id.button); final TextView tvInfo = (TextView)findViewById(R.id.text); butTest.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String string = null; try { Document doc = Jsoup.connect(" http://en.wikipedia.org/").get(); Elements newsHeadlines = doc.select("#mp-itn b a"); string = newsHeadlines.toString(); } catch (IOException e) { e.printStackTrace(); } tvInfo.setText(string); } }); } } ... |
|||
|
:
Нравится:
Не нравится:
|
|||
| 16.07.2016, 13:46 |
|
||
|
Загрузка текста с сайта
|
|||
|---|---|---|---|
|
#18+
DenisN03, вообще то надо задавать такие вещи на андроид форуме, пользоваться тегами форматирования текста 1 качаем библиотеку в bild.gradle app в секцию dependencies добавляем новую compile 'org.jsoup:jsoup:1.9.2' 2 добавляем разрешения в манифест <uses-permission android:name="android.permission.INTERNET" /> ну и сам код Код: java 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. ... |
|||
|
:
Нравится:
Не нравится:
|
|||
| 16.07.2016, 18:32 |
|
||
|
|

start [/forum/topic.php?fid=59&fpage=92&tid=2123886]: |
0ms |
get settings: |
6ms |
get forum list: |
14ms |
check forum access: |
2ms |
check topic access: |
2ms |
track hit: |
36ms |
get topic data: |
8ms |
get forum data: |
2ms |
get page messages: |
37ms |
get tp. blocked users: |
1ms |
| others: | 205ms |
| total: | 313ms |

| 0 / 0 |
