I am making this question today because I can not find certain information on google and/or Stack Overflow. My Problem is, I am creating a Android Application and I want certain information from a Website. So I use Jsoup as my parse Library. I follow all the instruction and set The data in the Text to Show up with The HTML text. For Some reason it is either a network connection or my code is wrong in communicating with the website? I do not know what is up with the app. I am Using Android Studio, so when running either the emulator or on a device it will just not pull html from the website.
Here is My code for 2 Classes. This has to be in Java.
package update.app.jdog1218.com.messingaround;
import android.app.Activity;
import android.app.DialogFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.net.URL;
import java.net.URLConnection;
public class MainActivity extends Activity {
private Button answer_Button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout bible = new RelativeLayout(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return super.onOptionsItemSelected(item);
}
public void gotoInbeddedBible(MenuItem item) {
Intent bibleclass = new Intent(this, bible.class);
final int result = 1;
bibleclass.putExtra("callingActivity", "MainActivity");
startActivity(bibleclass);
startActivityForResult(bibleclass, result);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
public void exit(MenuItem item) {
Button exitProcess = (Button) findViewById(R.id.submit);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
public void sotd(MenuItem item) {
Intent clickedSotd = new Intent(this, Sotd.class);
int result = 1;
startActivity(clickedSotd);
}
}
This is the class I want to use Jsoup in.
package update.app.jdog1218.com.messingaround;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.Menu;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.Connection;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
import java.io.IOException;
import static org.jsoup.Jsoup.*;
/*
* Created by Joel on 5/28/2015.
*/
public class Sotd extends Activity {
#Override
protected void onStart() {
super.onStart();
}
String title;
#Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.sotd);
Intent activityThatCalled = getIntent();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
/**
* Pull and Parse the HTML of Compass HB for SOTD.
*
* #return DocumentString
* #throws IOException
*/
protected String pullHTML() {
super.onStart();
final String url = "https://www.compasshb.com/api/v1/passages";
Document doc = null;
TextView textView = (TextView) findViewById(R.id.SOTD);
try {
doc = Jsoup.connect(url).get();
textView.setText(doc.ownText());
String paragraph = doc.title();
return paragraph;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostResume() {
super.onPostResume();
}
}
This is the XML File for the Layout I want to output too.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:weightSum="1"
android:background="#drawable/the_rendered_background">
<TextView
android:text="#string/text_of_activity_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="40dp"
android:capitalize="none"
android:width="200dp"
android:textColor="#000000"
android:id="#+id/text_view" />
</LinearLayout>
SOTD is where I want it to output too.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".Sotd"
android:weightSum="1"
android:background="#drawable/the_rendered_background"
android:backgroundTintMode="add">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/SOTD"
/>
</LinearLayout>
Pretty Much I just need someone to tell me what I am doing wrong./Review
Related
I have made an android app to display image from firebase into recycler view and when user click a image it has to go to its full screen page and have tried a several times but it just shows the blank activity
You can use PhotoView library (com.github.chrisbanes.photoview) to show full screen picture as below:
Piccaso is used here to show images, but you can use other image libraries such as Fresco, Glide .
ActivityLargeImageView
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.Window;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.github.chrisbanes.photoview.PhotoView;
import com.github.chrisbanes.photoview.PhotoViewAttacher;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import java.io.File;
import butterknife.BindView;
import butterknife.ButterKnife;
public class ActivityLargeImageView extends FragmentActivity {
#BindView(R.id.photoview_image)
PhotoView photoviewImage;
#BindView(R.id.progressbar)
ProgressBar progressBar;
File mFile;
PhotoViewAttacher mAttacher;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_large_image_view);
ButterKnife.bind(this);
progressBar.setVisibility(View.VISIBLE);
String img_url = getIntent().getExtras().getString("image_url");
try {
mFile = new File(img_url);
} catch (Exception ex) {
}
if (mFile.exists()) {
Picasso.with(getApplicationContext()).load(mFile).into(photoviewImage, imageLoadedCallback);
} else {
Picasso.with(getApplicationContext()).load(img_url).into(photoviewImage, imageLoadedCallback);
}
}
Callback imageLoadedCallback = new Callback() {
#Override
public void onSuccess() {
if (mAttacher != null) {
mAttacher.update();
} else {
mAttacher = new PhotoViewAttacher(photoviewImage);
}
progressBar.setVisibility(View.GONE);
}
#Override
public void onError() {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.error_message_connection_to_server), Toast.LENGTH_LONG).show();
}
};
}
XML File
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:background="#000000"
android:orientation="vertical" >
<com.github.chrisbanes.photoview.PhotoView
android:id="#+id/photoview_image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="#+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>
I am using google image search to look for images and get the URL of the first image in my android application using JSOUP library
My problem is that no matter how much I try It shows me that the URL is null
looks like the element that contains the first image does not have a url
here is the XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="arb.myapplication.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher"
android:layout_alignParentBottom="true"
android:layout_marginBottom="131dp"
android:id="#+id/imageView" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="39dp"
android:layout_marginEnd="39dp"
android:layout_marginTop="11dp"
android:id="#+id/button3"
android:elevation="0dp" />
Ignore the image view
I use the button to get the url of the image
here is the JAVA file
package arb.myapplication;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
class DownloadIcon extends AsyncTask<Object, Object, Integer>
{
#Override
protected Integer doInBackground(Object... objects) {
Connection connecion= Jsoup.connect("http://www.google.com/search?tbm=isch&q=facebook+logo");
try {
Document document=connecion.get();
Elements element=document.select("img.rg_ic.rg_i");
String url=element.first().absUrl("src");
publishProgress(url);
} catch (IOException e1) {
publishProgress(-1);
}
return 0;
}
#Override
protected void onProgressUpdate(Object... values) {
/* ImageView imageView=(ImageView) findViewById(R.id.imageView);
try {
InputStream inputStream= new URL(values[0].toString()).openStream();
imageView.setImageBitmap(BitmapFactory.decodeStream(inputStream));
} catch (IOException e) {
throw new RuntimeException(e);
}*/
Toast.makeText(getBaseContext(),values[0].toString(),Toast.LENGTH_LONG).show();
}
#Override
protected void onPostExecute(Integer integer) {
Toast.makeText(getBaseContext(),"done",Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button3=(Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new DownloadIcon().execute();
}
});
}
}
in the onProgressUpdate function does not show the Toast because its text is null
I already tried to get the attribute keys and values of the chosen element
It matches those displayed when I parse the element using the Chrome explorer but without src attribute
The text is null because you get no images. If you debug your request you'll see 403 status code which tells you that Jsoup client is not authorized to fetch images from Google. You need to use or emulate browser to achieve your goal.
With this code, as I while scrolling, say 10%, disable web.reload (); And for one, as I variable String url of shouldOverrideUrlLoading endure beyond the example as above in a variable String XYvariable.
package ru.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
TextView TEXT;
String XYvariable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView web = (WebView) findViewById(R.id.webView);
web.loadUrl("http://example.com/string.html");
web.getSettings().setJavaScriptEnabled(true);
new Timer().schedule(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
web.reload();
}
}, 1000, 1000);
web.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String Cur = (String) url.substring(23);
String Link = (String) url.substring(16,23);
String XY = (String) url; // Post varriable up in string XYvariable
setContentView(R.layout.vot);
TEXT = (TextView) findViewById(R.id.textView);
switch (Link){
case "id_user":
TEXT.setText(Cur+" / user");
break;
case "id_mess":
TEXT.setText(Cur + " / messages");
break;
}
return true;
}
});
}
public boolean onCreateOptionsMenu (Menu menu){
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ru.MainActivity">
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/webView"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
In advance thank you all, very much! ))
I'm having a bit of an issue, I'm working on a android app that requires a spinner to update a string to your spinner selection, I have it all working and everything, the problem is that I need the spinner on another activity than the starting activity but when I start the app with a different activity than the one with the spinner and activate the array adapter and listener at the same time I switch views to the one with the spinner, the spinner is empty with no choices. I can't for the life of me figure this out. PS, sorry about the weird variable names I get bored and instead of making a proper name I think of something on the spot and come back and change it later. PPS The name of the content view with the spinner is orderpage and the one I want to have start with the app is activity_main PPPS I cut out some methods I deemed irrelevant so there are some variables not used. I attached my code below:
package com.NIQUOLI.Deliveryapp;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.NIQUOLI.Deliveryapp.GMailSender;
import android.R.string;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Debug;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
public static boolean deeznuts = false;
public static String storenamestring;
public static String namesame;
public static String instructionsame;
public int choice = 0;
private Spinner spinner1;
private Button button = null;
String[] stores;
Spinner sp;
public void cont(View View){
deeznuts = true;
setContentView(R.layout.orderpage);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.orderpage);
button = (Button) findViewById(R.id.send_email);
super.onCreate(savedInstanceState);
sp = (Spinner) findViewById(R.id.sp);
stores = getResources().getStringArray(R.array.stores);
if (deeznuts = true){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,stores);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
int index = arg0.getSelectedItemPosition();
Toast.makeText(getBaseContext(), "You selected"+stores[index], Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
parent.getItemAtPosition(pos);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and of course my XML with the s[ommer:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.NIQUOLI.Deliveryapp.SendMailTask" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="#string/order_page"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center_horizontal"
android:hint="#string/enter_your_name"
android:inputType="text" >
<requestFocus />
</EditText>
<Spinner
android:id="#+id/sp"
android:layout_width="388dp"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/instructions"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.34"
android:ems="10"
android:inputType="textMultiLine" />
<Button
android:id="#+id/send_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="#string/send_order" />
</LinearLayout>
On the 6th line of the onCreate method in the first code sample, if(deeznuts = true) should be if(deeznuts == true).
I am trying to get this right, but this is getting me good. I have an example but i don't know how it is working.
I got this much done.
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true" >
</ListView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/welcome" />
</RelativeLayout>
now I think my problem is in this java file. The getListView thing is kicking me around. I think this is the answer but i just don't know how to get this right.
I think i know how to get it to work later.
package com.example.boonehallfrightnightsapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
static final String[] CHOICES = new String[]
{
"Main House",
"Nightmare",
"Zombie Town",
"Hayride",
"Quit"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//found this part on an example
//Set up ArrayAdaptor for the options
setListAdapter(new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, CHOICES));
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setTextFilterEnabled(true);
}
private void setListAdapter(ArrayAdapter<String> arrayAdapter) {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void setListClickListener()
{
//Set up the click listener for the options
getListView().setOnItemClickListener
(
new OnItemClickListener()
{
//#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
switch(arg2)
{
case 0: finish();
break;
case 1: finish();
break;
case 2: finish();
break;
case 3: finish();
break;
case 4: finish();
break;
default: break;
}
}
}//END OnItemClickListener
);//END setOnItemClickListener
}//END setListClickListener
}
You should extend ListActivity to call getListView() to get the ListView from the layout. You can check the documentation here - http://developer.android.com/reference/android/app/ListActivity.html (Class Overview > Screen Layout)