I am having two android applications..The first of them consists of an edit text with a button and an image view ...clicking the button will invoke a connection to website and download an image to the image view...This applications works well this is the xml of first application:
<?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="match_parent"
app:srcCompat="@mipmap/ic_launcher"
android:layout_alignParentBottom="true"
android:layout_marginBottom="131dp"
android:id="@+id/imageView"
android:visibility="invisible"/>
<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" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_below="@+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="26dp"
android:layout_marginStart="26dp"
android:layout_marginTop="47dp"
android:id="@+id/editText"
android:background="@android:color/background_light"
android:textColor="@android:color/background_dark" />
</RelativeLayout>
and here is the java file of the first application
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<String, Object, Integer>
{
String uri="";
public DownloadIcon(String s) {uri=s;
}
@Override
protected Integer doInBackground(String... objects) {
Connection connecion= Jsoup.connect("http://icons.better-idea.org/icons?url="+uri+"#result");
Document document= null;
try {
document = connecion.get();
} catch (IOException e) {
e.printStackTrace();
}
Element element=document.select("td.url").first().child(0);
String url=element.attr("href");
InputStream inputStream= null;
try {
inputStream = new URL(url).openStream();
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
publishProgress(bitmap);
return 0;
}
@Override
protected void onProgressUpdate(Object... values) {
ImageView imageView=(ImageView) findViewById(R.id.imageView);
imageView.setVisibility(View.VISIBLE);
Bitmap bitmap=(Bitmap) values[0];
imageView.setImageBitmap(bitmap);
//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) {
EditText editText=(EditText) findViewById(R.id.editText);
new DownloadIcon(editText.getText().toString()).execute();
}
});
}
}
I copied the code of downloading the image to the second application and did some small changes to it but it is not working well any more This is the xml code which is not important as the download process runs when calling onResume()
<?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.passwordmanager.MainActivity"
android:background="@drawable/green_background">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/grid">
</android.support.v7.widget.RecyclerView>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:fabSize="mini"
app:srcCompat="@android:drawable/ic_menu_add"
android:id="@+id/floatingActionButton"
app:elevation="0dp"
app:backgroundTint="#66FF66"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:src="@android:drawable/ic_delete"
android:id="@+id/delete"/>
</RelativeLayout>
this is the java file
package arb.passwordmanager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.provider.ContactsContract;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import yellow5a5.actswitchanimtool.ActSwitchAnimTool;
public class MainActivity extends AppCompatActivity {
Menu mMenu;
void check_password_checked()
{
Boolean b=getIntent().getExtras().getBoolean("checked");
if(!b)finish();
}
void receive_animation()
{
if(getIntent().getExtras().getBoolean("animation"))
{
FloatingActionButton fab=(FloatingActionButton) findViewById(R.id.floatingActionButton);
new ActSwitchAnimTool(this)
.receiveIntent(getIntent())
.setAnimType(ActSwitchAnimTool.MODE_SHRINK)
.target(fab)
.build();
}
}
void set_fab_click()
{
FloatingActionButton fab=(FloatingActionButton) findViewById(R.id.floatingActionButton);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FloatingActionButton fab=(FloatingActionButton) findViewById(R.id.floatingActionButton);
Intent i=new Intent(getBaseContext(),add_account_activity.class);
ActSwitchAnimTool asat=new ActSwitchAnimTool(MainActivity.this).setAnimType(0)
.target(fab)
.setShrinkBack(false)
.setmColorStart(Color.parseColor("#66FF66"))
.setmColorEnd(Color.parseColor("#66FF66"))
.startActivity(i,false)
;
asat.setAnimType(0).build();
}
});
}
GridLayoutManager gridLayoutManager;
RecyclerView recyclerView;
List <Item> items;
void set_spans()
{
SharedPreferences sharedPreferences=getSharedPreferences("view",MODE_PRIVATE);
Boolean b=sharedPreferences.getBoolean("grid",true);
if(b)gridLayoutManager=new GridLayoutManager(this,3);
else gridLayoutManager=new GridLayoutManager(this,1);
recyclerView=(RecyclerView) findViewById(R.id.grid);
recyclerView.setLayoutManager(gridLayoutManager);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
check_password_checked();
receive_animation();
set_fab_click();
set_spans();
}
class DownloadIcon extends AsyncTask<Object, Object, Integer>
{
//String uri="";
//public DownloadIcon(String s) {uri=s;
//}
@Override
protected Integer doInBackground(Object... objects) {
Connection connecion= Jsoup.connect("http://www.icons.better-idea.org/icons?url="+objects[0].toString()+"#result");
try {
Document document= connecion.get();
Element element=document.select("td.url").first().child(0);
String url=element.attr("href");
InputStream inputStream = new URL(url).openStream();
Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
//publishProgress(bitmap);
} catch (IOException e) {
publishProgress("http://www.icons.better-idea.org/icons?url="+objects[0].toString()+"#result");
e.printStackTrace();
}
return 0;
}
@Override
protected void onProgressUpdate(Object... values) {
/* ImageView imageView=(ImageView) findViewById(R.id.imageView);
imageView.setVisibility(View.VISIBLE);
Bitmap bitmap=(Bitmap) values[0];
imageView.setImageBitmap(bitmap);*/
//Toast.makeText(getBaseContext(),values[0].toString(),Toast.LENGTH_LONG).show();
Toast.makeText(getBaseContext(),values[0].toString(),Toast.LENGTH_LONG).show();
}
@Override
protected void onPostExecute(Integer integer) {
Toast.makeText(getBaseContext(),"done",Toast.LENGTH_SHORT).show();
}
}
String correct_url(String s)
{
s=s.replace(" ","");
if(!s.contains("."))s+=".com";
else if(s.lastIndexOf(".")<s.length()-4)s+=".com";
return s;
}
void init_data()
{
database d=new database(this,database.DATABASE_NAME,null,database.DATABASE_VERSION);
SQLiteDatabase sqLiteDatabase=d.getReadableDatabase();
Cursor cursor=sqLiteDatabase.rawQuery("SELECT * FROM "+database.TABLE_ACCOUNTS,null);
items=new ArrayList<>(cursor.getCount());
if(cursor.getCount()>0)
{
cursor.moveToFirst();
do {
File file =new File(String.valueOf(getFilesDir()),cursor.getString(0));
Bitmap b =null;
try {
b= BitmapFactory.decodeStream(new FileInputStream(file));
items.add(new Item(cursor.getString(0),cursor.getString(1),b));
} catch (FileNotFoundException e) {
new DownloadIcon().execute(correct_url(cursor.getString(0)),b);
}
//items.add(new Item(cursor.getString(0),cursor.getString(1)))
}while (cursor.moveToNext());
}
}
@Override
protected void onResume() {
super.onResume();
init_data();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
mMenu=menu;
SharedPreferences sharedPreferences=getSharedPreferences("view",MODE_PRIVATE);
Boolean b= sharedPreferences.getBoolean("grid",true);
if(b)getMenuInflater().inflate(R.menu.edit_password,menu);
else getMenuInflater().inflate(R.menu.list_view,menu);
return true;
}
void settings()
{
Intent i=new Intent(this,Settings.class);
i.putExtra("checked",true);
startActivity(i);
}
void fab_click()
{
FloatingActionButton fab=(FloatingActionButton) findViewById(R.id.floatingActionButton);
fab.performClick();
}
void list_view()
{
mMenu.clear();
getMenuInflater().inflate(R.menu.edit_password,mMenu);
SharedPreferences sharedPreferences=getSharedPreferences("view",MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit().putBoolean("grid",true);
editor.commit();
gridLayoutManager.setSpanCount(3);
}
void grid_view()
{
mMenu.clear();
getMenuInflater().inflate(R.menu.list_view,mMenu);
SharedPreferences sharedPreferences=getSharedPreferences("view",MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit().putBoolean("grid",false);
editor.commit();
gridLayoutManager.setSpanCount(1);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.settings_menu :
settings();
return true;
case R.id.add_new_account_menu:
fab_click();
return true;
case R.id.list_view:
list_view();
return true;
case R.id.grid_view:
grid_view();
return true;
default: return true ;
}
}
}
the only important part is the init_data()
void that runs in onResume() it checks the database for specific data and search it and downloads image I used "facebook.com" as a parameter to send to the async task but it is not working although it works in the first application
it returns the folowing error
W/System.err: java.net.UnknownHostException: Unable to resolve host "www.icons.better-idea.org": No address associated with hostname
W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
W/System.err: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:214)
W/System.err: at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
W/System.err: at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
W/System.err: at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:362)
W/System.err: at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
W/System.err: at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
W/System.err: at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
W/System.err: at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
W/System.err: at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
W/System.err: at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
W/System.err: at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
W/System.err: at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:652)
W/System.err: at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:629)
W/System.err: at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:261)
W/System.err: at org.jsoup.helper.HttpConnection.get(HttpConnection.java:250)
W/System.err: at arb.passwordmanager.MainActivity$DownloadIcon.doInBackground(MainActivity.java:125)
W/System.err: at arb.passwordmanager.MainActivity$DownloadIcon.doInBackground(MainActivity.java:111)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:287)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:234)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
W/System.err: at java.lang.Thread.run(Thread.java:856)
W/System.err: Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
W/System.err: at libcore.io.Posix.getaddrinfo(Native Method)
W/System.err: at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:59)
W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
W/System.err: ... 24 more
W/System.err: Caused by: libcore.io.ErrnoException: getaddrinfo failed: ENOENT (No such file or directory)
W/System.err: ... 27 more
D/HAWAII_EGL: eglMakeCurrent(0x40c84968, 0x4c1a4cf0, 0x4c1a4cf0) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(0x40c84968, 0x4c1a4cf0, 0x4c1a4cf0) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(0x40c84968, 0x4c1a4cf0, 0x4c1a4cf0) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(0x40c84968, 0x4c1a4cf0, 0x4c1a4cf0) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 4807
D/dalvikvm: GC_EXPLICIT freed 2905K, 21% free 14545K/18344K, paused 4ms+4ms, total 37ms
D/HAWAII_EGL: eglMakeCurrent(0x40c84968, 0x4c1a4cf0, 0x4c1a4cf0) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(0x40c84968, 0x4c1a4cf0, 0x4c1a4cf0) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(0x40c84968, 0x4c1a4cf0, 0x4c1a4cf0) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(0x40c84968, 0x4c1a4cf0, 0x4c1a4cf0) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(0x40c84968, 0x4c1a4cf0, 0x4c1a4cf0) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(0x40c84968, 0x4c1a4cf0, 0x4c1a4cf0) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(0x40c84968, 0x4c1a4cf0, 0x4c1a4cf0) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(0x40c84968, 0x4c1a4cf0, 0x4c1a4cf0) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(0x40c84968, 0x4c1a4cf0, 0x4c1a4cf0) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(0x40c84968, 0x4c1a4cf0, 0x4c1a4cf0) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(0x40c84968, 0x4c1a4cf0, 0x4c1a4cf0) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(0x40c84968, 0x4c1a4cf0, 0x4c1a4cf0) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(0x40c84968, 0x4d6c1ab8, 0x4d6c1ab8) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 4807
D/HAWAII_EGL: eglDestroySurface() surface: 0x4d6c1ab8, android window 0x4c056218, Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(0x40c84968, 0x4c1a4cf0, 0x4c1a4cf0) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(0x40c84968, 0x4c1a4cf0, 0x4c1a4cf0) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(0x40c84968, 0x4c1a4cf0, 0x4c1a4cf0) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 4807
D/HAWAII_EGL: eglMakeCurrent(0x40c84968, 0x4c1a4cf0, 0x4c1a4cf0) Thread: 4807
D/HAWAII_EGL: eglDestroyContext() context: 0x40c84968, VC context: 1, Thread 4807
E/HAWAII_EGL: Destroying surface without window
D/HAWAII_EGL: Destroying context in eglMakeCurrent
D/HAWAII_EGL: eglMakeCurrent(NULL) Thread: 4807
Disconnected from the target VM, address: 'localhost:8601', transport: 'socket'
I searched for a solution for this problem and find one of the following: I should include internet uses permission which I did Then I should provide a good internet connection and make sure the server is working But the first application is working fine while the other does not although they use the same internet connection and connects to the same server and use the same url help me please
Try your link without "www" I mean this: "http://www.icons.better-idea.org" I think there is just a typo in your code
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments