i'm new in android developing and i was looking for a way to download pictures and text from the internet to my app. I found a few tutorials and included the code in my project. When i'm running the app on the emulator everything just work fine. If i run that app on my Device (Samsung Galaxy S4) i get the "connecting Error" (in the HttpDownload.java) and i have no idea why, cause in the other apps the internet is working.
Thank u for the help.
Here are the Codes:
HttpDownload.java:
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class HttpDownload extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private InputStream OpenHttpConnection(String urlString)
throws IOException
{
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
}
catch (Exception ex)
{
throw new IOException("Error connecting");
}
return in;
}
public Bitmap DownloadImage(String URL)
{
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}
public String DownloadText(String URL)
{
int BUFFER_SIZE = 2000;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return "";
}
InputStreamReader isr = new InputStreamReader(in);
int charRead;
String str = "";
char[] inputBuffer = new char[BUFFER_SIZE];
try {
while ((charRead = isr.read(inputBuffer))>0)
{
//---convert the chars to a String---
String readString =
String.copyValueOf(inputBuffer, 0, charRead);
str += readString;
inputBuffer = new char[BUFFER_SIZE];
}
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
return str;
}
}
main_activity:
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
TextView meinText;
ImageView meinBild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
meinBild = (ImageView)findViewById(R.id.meinBild);
meinText = (TextView)findViewById(R.id.meinText);
}
public void laden (View view) {
HttpDownload download = new HttpDownload();
meinBild.setImageBitmap(download.DownloadImage("http://www.queness.com/resources/images/png/apple_ex.png"));
}
}
Logcat:
Local Branch:
Remote Branch:
Local Patches:
Reconstruct Branch:
04-29 16:36:28.757 32038-32038/com.example.itsme.internetdaten D/OpenGLRenderer﹕ Enabling debug mode 0
04-29 16:36:53.862 32038-32038/com.example.itsme.internetdaten W/System.err﹕ java.io.IOException: Error connecting
04-29 16:36:53.862 32038-32038/com.example.itsme.internetdaten W/System.err﹕ at com.example.itsme.internetdaten.HttpDownload.OpenHttpConnection(HttpDownload.java:50)
04-29 16:36:53.862 32038-32038/com.example.itsme.internetdaten W/System.err﹕ at com.example.itsme.internetdaten.HttpDownload.DownloadImage(HttpDownload.java:60)
04-29 16:36:53.862 32038-32038/com.example.itsme.internetdaten W/System.err﹕ at com.example.itsme.internetdaten.MainActivity.laden(MainActivity.java:29)
04-29 16:36:53.862 32038-32038/com.example.itsme.internetdaten W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
04-29 16:36:53.862 32038-32038/com.example.itsme.internetdaten W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
04-29 16:36:53.862 32038-32038/com.example.itsme.internetdaten W/System.err﹕ at android.view.View$1.onClick(View.java:3964)
04-29 16:36:53.862 32038-32038/com.example.itsme.internetdaten W/System.err﹕ at android.view.View.performClick(View.java:4633)
04-29 16:36:53.862 32038-32038/com.example.itsme.internetdaten W/System.err﹕ at android.view.View$PerformClick.run(View.java:19330)
04-29 16:36:53.862 32038-32038/com.example.itsme.internetdaten W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
04-29 16:36:53.862 32038-32038/com.example.itsme.internetdaten W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
04-29 16:36:53.862 32038-32038/com.example.itsme.internetdaten W/System.err﹕ at android.os.Looper.loop(Looper.java:157)
04-29 16:36:53.862 32038-32038/com.example.itsme.internetdaten W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5356)
04-29 16:36:53.862 32038-32038/com.example.itsme.internetdaten W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
04-29 16:36:53.862 32038-32038/com.example.itsme.internetdaten W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
04-29 16:36:53.882 32038-32038/com.example.itsme.internetdaten W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
04-29 16:36:53.882 32038-32038/com.example.itsme.internetdaten W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
04-29 16:36:53.882 32038-32038/com.example.itsme.internetdaten W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
04-29 16:36:53.882 32038-32038/com.example.itsme.internetdaten I/Choreographer﹕ Skipped 61 frames! The application may be doing too much work on its main thread.
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.itsme.internetdaten" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>