Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,809,576 Members 37,543 Now Online
XDA Developers Android and Mobile Development Forum

['Http Connection'] App is crashing

Tip us?
 
hiphop12ism
Old
#1  
hiphop12ism's Avatar
Member - OP
Thanks Meter 2
Posts: 54
Join Date: Aug 2012
Default ['Http Connection'] App is crashing

Hello guys,

Well, I was working with data sharing via http. So, I made an app in which the app downloads a image after a certain button is clicked. However the app is crashing at that moment.

But when I execute
Code:
new DownloadImageTask().execute(myUrl);
from OnCreate, the app just works fine. I am completely lost here. Please help me out!
I am giving you the MainActivity.class and the logcat.
Thanks

MainActivity.class
Code:
package com.example.neew;

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

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements OnClickListener{
	ImageView img;
	TextView text;
	Thread t;
	Button b;
	Handler handler=new Handler();
	String myUrl="http://s27.postimg.org/epsajh82q/Nissan_Skyline_Wallpaper_Photos_HD.jpg";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		img=(ImageView)findViewById(R.id.images);
		text=(TextView)findViewById(R.id.tex);
		b=(Button)findViewById(R.id.button1);
		b.setOnClickListener(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.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);
	}
	private InputStream openHttpConnection(String urlString) throws IOException{
		InputStream in=null;
		int response=-1;
		URL url=new URL(urlString);
		URLConnection urlconnection=url.openConnection();
		if(!(urlconnection instanceof HttpURLConnection )){
			Toast.makeText(getBaseContext(),
					"Not an Http protocol",
					Toast.LENGTH_LONG).show();
		}else{
			try{
			HttpURLConnection conn=(HttpURLConnection)urlconnection;
			conn.setAllowUserInteraction(false);
			conn.setInstanceFollowRedirects(true);
			conn.setRequestMethod("GET");
			conn.connect();
			text.setText("conn made");
			response=conn.getResponseCode();
			if(response==HttpURLConnection.HTTP_OK){
				//text.setText("Connection made-Protocol accepted");
				runner("Protocol is setuped");
					in=conn.getInputStream();
			}
			}catch(Exception e){
				Toast.makeText(getBaseContext(), e.toString(), 3000).show();
			}
			}
		//text.setText("Stream returned");
			runner("stream is returned");
		return in;
		}
		private Bitmap downloadImage(String url){
			Bitmap bitmap=null;
			InputStream input=null;
			try{
				input=openHttpConnection(url);
				bitmap=BitmapFactory.decodeStream(input);
				
				input.close();
			}catch(IOException e){
				Toast.makeText(getApplicationContext(), e.toString(), 3467).show();
			}
			runner("Bitmap is downloaded");
			return bitmap;
		}
		
		public void Theader(final String msg){
			t=new Thread(new Runnable(){
				public void run(){
					handler.post(new Runnable(){
						public void run(){
							text.setText(msg);
						}
					});				
				
				}
			});
			t.start();
		}
		
		@SuppressWarnings("deprecation")
		public void runner(String s){
			
			Theader(s);
			if(text.getText().toString()==s)
			{
				t.stop();
			}
		}
		
		private class DownloadImageTask extends AsyncTask<String,Void,Bitmap>{

			@Override
			protected Bitmap doInBackground(String... params) {
				// TODO Auto-generated method stub
				runner("Downloading bitmap");
				return downloadImage(params[0]);
			}
			
			protected void onPostExecute(Bitmap result){
				
				runner("Bitmap is set");
				
				img.setImageBitmap(result);
				
			}
			
		}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			new DownloadImageTask().execute(myUrl);
			
		}
		
		
	}
__________________________________________________ ______________________________________________


Logcat


Code:
08-16 09:12:22.442: I/dalvikvm(707): threadid=3: reacting to signal 3
08-16 09:12:22.518: I/dalvikvm(707): Wrote stack traces to '/data/anr/traces.txt'
08-16 09:12:22.962: I/dalvikvm(707): threadid=3: reacting to signal 3
08-16 09:12:23.022: I/dalvikvm(707): Wrote stack traces to '/data/anr/traces.txt'
08-16 09:12:23.481: I/dalvikvm(707): threadid=3: reacting to signal 3
08-16 09:12:23.511: I/dalvikvm(707): Wrote stack traces to '/data/anr/traces.txt'
08-16 09:12:23.631: D/gralloc_goldfish(707): Emulator without GPU emulation detected.
08-16 09:25:43.031: W/dalvikvm(707): threadid=11: thread exiting with uncaught exception (group=0x2ba041f8)
08-16 09:25:43.061: E/AndroidRuntime(707): FATAL EXCEPTION: AsyncTask #1
08-16 09:25:43.061: E/AndroidRuntime(707): java.lang.RuntimeException: An error occured while executing doInBackground()
08-16 09:25:43.061: E/AndroidRuntime(707): 	at android.os.AsyncTask$3.done(AsyncTask.java:278)
08-16 09:25:43.061: E/AndroidRuntime(707): 	at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-16 09:25:43.061: E/AndroidRuntime(707): 	at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-16 09:25:43.061: E/AndroidRuntime(707): 	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-16 09:25:43.061: E/AndroidRuntime(707): 	at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-16 09:25:43.061: E/AndroidRuntime(707): 	at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
08-16 09:25:43.061: E/AndroidRuntime(707): 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-16 09:25:43.061: E/AndroidRuntime(707): 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-16 09:25:43.061: E/AndroidRuntime(707): 	at java.lang.Thread.run(Thread.java:856)
08-16 09:25:43.061: E/AndroidRuntime(707): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-16 09:25:43.061: E/AndroidRuntime(707): 	at android.os.Handler.<init>(Handler.java:121)
08-16 09:25:43.061: E/AndroidRuntime(707): 	at android.widget.Toast$TN.<init>(Toast.java:317)
08-16 09:25:43.061: E/AndroidRuntime(707): 	at android.widget.Toast.<init>(Toast.java:91)
08-16 09:25:43.061: E/AndroidRuntime(707): 	at android.widget.Toast.makeText(Toast.java:233)
08-16 09:25:43.061: E/AndroidRuntime(707): 	at com.example.neew.MainActivity.openHttpConnection(MainActivity.java:86)
08-16 09:25:43.061: E/AndroidRuntime(707): 	at com.example.neew.MainActivity.downloadImage(MainActivity.java:97)
08-16 09:25:43.061: E/AndroidRuntime(707): 	at com.example.neew.MainActivity.access$0(MainActivity.java:93)
08-16 09:25:43.061: E/AndroidRuntime(707): 	at com.example.neew.MainActivity$DownloadImageTask.doInBackground(MainActivity.java:138)
08-16 09:25:43.061: E/AndroidRuntime(707): 	at com.example.neew.MainActivity$DownloadImageTask.doInBackground(MainActivity.java:1)
08-16 09:25:43.061: E/AndroidRuntime(707): 	at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-16 09:25:43.061: E/AndroidRuntime(707): 	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-16 09:25:43.061: E/AndroidRuntime(707): 	... 5 more
08-16 09:25:47.902: I/Process(707): Sending signal. PID: 707 SIG: 9
 
hiphop12ism
Old
#2  
hiphop12ism's Avatar
Member - OP
Thanks Meter 2
Posts: 54
Join Date: Aug 2012
Wohooooo !!!is anybody there

Sent from my GT-P3100 using XDA Free mobile app
 
Jonny
Old
#3  
Jonny's Avatar
Forum Moderator
Thanks Meter 8,002
Posts: 7,981
Join Date: Jul 2011
Location: Cheshire

 
DONATE TO ME
Quote:
Originally Posted by hiphop12ism View Post
Wohooooo !!!is anybody there

Sent from my GT-P3100 using XDA Free mobile app
If you read the logcat it tells you the exact line in your code that is causing the error.

Sent from my HTC One using Tapatalk
 
Gohan
Old
#4  
Gohan's Avatar
Senior Member
Thanks Meter 959
Posts: 9,990
Join Date: May 2012
Location: Nowhere
Did you give your app permission to access the internet?
 
hiphop12ism
Old
#5  
hiphop12ism's Avatar
Member - OP
Thanks Meter 2
Posts: 54
Join Date: Aug 2012
Yes i have

Sent from my GT-P3100 using XDA Free mobile app
 
Gohan
Old
#7  
Gohan's Avatar
Senior Member
Thanks Meter 959
Posts: 9,990
Join Date: May 2012
Location: Nowhere
Quote:
Originally Posted by SimplicityApks View Post
This is not the way I'd normally answer, but my advice is to use volley for all internet things. They even had an IO talk about it
Is this for downloading or can it be used to send data to POST php scripts?
 
SimplicityApks
Old
#8  
SimplicityApks's Avatar
Senior Member
Thanks Meter 327
Posts: 323
Join Date: May 2013
Location: Aachen
Quote:
Originally Posted by Gohan View Post
Is this for downloading or can it be used to send data to POST php scripts?
You should be able to use it for both, at some point you select if you want httpPost or get and if you want it to be a String or JSON or some other request.
 
huy8687
Old
#9  
Junior Member
Thanks Meter 0
Posts: 13
Join Date: Jan 2011
Can't create handler inside thread that has not called Looper.prepare()

you should create a handler as a field, call its when you need, not create new hanlder in thread
 
hiphop12ism
Old
#10  
hiphop12ism's Avatar
Member - OP
Thanks Meter 2
Posts: 54
Join Date: Aug 2012
But it is working perfectly fine when i execute asynctask from onCreate.

Ps: sorry I am quite new to multithreading

Sent from my GT-P3100 using XDA Free mobile app

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes