Data Parser! Android



  • That's the logic. From another activate, we're transmitting a reference that needs to be fed. This is where we pull it and show it on the screen. But why does it make a mistake when I want to move on? If the line is fixed new ParsePost().execute(); That's all right, but no data. Please... I can't find a mistake! Code:

        package com.group.samuliak.hpk.activity;
    

    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.AsyncTask;
    import android.support.design.widget.CollapsingToolbarLayout;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.ImageView;
    import android.widget.TextView;

    import com.group.samuliak.hpk.R;

    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.select.Elements;

    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;

    public class detailPost extends AppCompatActivity {
    public final static String ID = "ID";

    TextView title, text;
    ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail_post);
    CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
    collapsingToolbarLayout.setTitle("Collapsing");
    collapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(android.R.color.transparent));

    title = (TextView) findViewById(R.id.title);
    text = (TextView) findViewById(R.id.text);
    iv = (ImageView) findViewById(R.id.image);
    
    new ParsePost().execute();
    

    }

    public class ParsePost extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... arg) {
    String myURL = String.valueOf(getIntent().getIntExtra(ID, 0));
    parsePost(myURL);
    return null;
    }

    private void parsePost(String myURL) {
        Elements titleParse, textParse, photoParse;
        try {
            Document doc  = Jsoup.connect(myURL).get();
            titleParse =  doc.select("h1.page-title");
            textParse = doc.select("div.news-body p");
            photoParse = doc.select("figure.news-featured-image img");
            String myURLPhoto = photoParse.attr("src");
            InputStream is = (InputStream) new URL(myURLPhoto).getContent();
            Bitmap bitmap = BitmapFactory.decodeStream(is);
    
            StringBuilder stringBuilder = new StringBuilder();
    
            for(int i = 0; i &lt; textParse.size(); i++) {
                stringBuilder.append(textParse.first());
                textParse.remove(0);
            }
    
            iv.setImageBitmap(bitmap);
            title.setText(titleParse.text());
            text.setText(stringBuilder.toString());
        } catch (IOException e) {}
    }
    
    @Override
    protected void onPostExecute(String result) {}
    

    }

    Mistakes:

    01-11 18:11:54.495 12839-12943/com.group.samuliak.hpk
    E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
    java.lang.RuntimeException: An error occured while executing
    doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:299)
    at
    java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    at
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    at
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    at java.lang.Thread.run(Thread.java:856)
    Caused by: java.lang.IllegalArgumentException: Malformed URL: 0
    at org.jsoup.helper.HttpConnection.url(HtpConnection.java:78)
    at org.jsoup.helper.HttpConnection.connect(HtpConnection.java:38)
    at org.jsoup.Jsoup.connect(Jsoup.java:73)
    at
    com.group.samuliak.hpk.activity.detailPost$ParsePost.parsePost(detailPost.java:55)
    at
    com.group.samuliak.hpk.activity.detailPost$ParsePost.doInBackground(detailPost.java:48)
    at
    com.group.samuliak.hpk.activity.detailPost$ParsePost.doInBackground(detailPost.java:44)
    at android.os.AsyncTask$2.call(AsyncTask.java:287)
    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    at
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    at
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    at java.lang.Thread.run(Thread.java:856)
    Caused by: java.net.MalformedURLException: Protocol not found: 0
    at java.net.URL.(URL.java:178)
    at java.net.URL.(URL.java:127)
    at org.jsoup.helper.HttpConnection.url(HtpConnection.java:76)
    at org.jsoup.helper.HttpConnection.connect(HtpConnection.java:38)
    at org.jsoup.Jsoup.connect(Jsoup.java:73)
    at
    com.group.samuliak.hpk.activity.detailPost$ParsePost.parsePost(detailPost.java:55)
    at
    com.group.samuliak.hpk.activity.detailPost$ParsePost.doInBackground(detailPost.java:48)
    at
    com.group.samuliak.hpk.activity.detailPost$ParsePost.doInBackground(detailPost.java:44)
    at android.os.AsyncTask$2.call(AsyncTask.java:287)
    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    at
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    at
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    at java.lang.Thread.run(Thread.java:856) 01-11 18:11:54.515
    12839-12839/com.group.samuliak.hpk W/EGL_genymotion: eglSurfaceAttrib
    not implemented


  • QA Engineer

    Look how you shape. URL:

    String myURL = String.valueOf(getIntent().getIntExtra(ID, 0));

    Method public int getIntExtra (String name, int defaultValue) http://developer.android.com/reference/android/content/Intent.html#getIntExtra%28java.lang.String,%20int%29 Total value. Accordingly, this problem has been identified in the scope of the exclusion:

    Caused by: java.net.MalformedURLException: Protocol not found: 0 at java.net. URL




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2