Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi Guys !
im trying to make simple app to connect internet. but it doesn't work !
& i dont know why ?! i also searched Google to find Simple exampels but i didnt find anything usefull .
please help me and fix my mistakes .
tnx


error
:
java.lang.NullPointerException: println needs a message
W/IInputConnectionWrapper(2880): showStatusIcon on inactive InputConnection



Code :

Java
package net.learn2develop.http3;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		
		
		
		Log.d("location", " 0 as start");
		
		InputStream in=null;
		int response=-1;
		String urlString="http://www.tabnak.ir";
		try
		{
		URL url=new URL(urlString);
		URLConnection conn=url.openConnection();
		if(!(conn instanceof  HttpURLConnection))
		throw new IOException("Not ann 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();}
			else{ 
				Log.d("location", " 1");
				}
			TextView intxt=(TextView)findViewById(R.id.mytxt); 
			intxt.setText(in.toString());
			
		}
		catch(Exception ex0)
		{
			Log.d("e:",ex0.getLocalizedMessage());
		}
		
		}
		catch(Exception ex)
		{
			Log.d("location 2 Exception : ", ex.toString());
		}
		
		
		

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		
		
	}



AndroidManifest.xml :

XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.learn2develop.http3"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
 <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="net.learn2develop.http3.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>


UI :

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" >

    <TextView
        android:id="@+id/mytxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>
Posted
Updated 30-Nov-14 4:58am
v3
Comments
DamithSL 30-Nov-14 11:50am    
try to dispose the stream

intxt.setText(in.toString());
in.close();;
seyed mahmud shahrokni 30-Nov-14 12:18pm    
it doesnt work !

java.lang.NullPointerException

One of the basic errors that you need to understand. You have a reference object somewhere that has not been initialized to refer to anything. Use your debugger to find out where it is.

Also, your code would be much easier to read if you made better use of spaces and newlines.
 
Share this answer
 
Comments
seyed mahmud shahrokni 30-Nov-14 12:26pm    
have you ever read my Code ?!
do you know anything about Difference between suggestion and Solution !?
if you have a suggestion you can use Comments , right ?
sorry man but i want a solution ! a peace of code you know !
i know how can i use my debugger ! and i did that !
but i cant find problem !
its an easy question for someone like you , but im new in android ! ok ?
i know you can solve it in a minute ! but you dont want .
Richard MacCutchan 1-Dec-14 3:14am    
Vitriolic attacks such as this will not endear you to this community.
Richard MacCutchan 1-Dec-14 3:18am    
Yes I have read your code, and somewhere in there you have a NULL pointer, which is impossible to identify without using the debugger. But if you are too lazy to make the effort why do you think anyone else would?
Move your HTTP connection code to a worker thread. The network I/O will cause crash in UI thread (onCreate).
 
Share this answer
 
I solved it mySelf !

Java
package com.example.html5;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import java.net.URL;
import java.net.URLConnection;
import java.net.HttpURLConnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.widget.TextView;
import android.util.Log;
import android.os.AsyncTask;

public class MainActivity extends Activity {
	
	
	
	
	
	protected String ShowHtml()
	{
		//TextView myTxt=(TextView)findViewById(R.id.txtReturn); 
		HttpURLConnection urlConnection = null;
		String line="Start:";
		try
		{
		URL myUrl=new URL("http://google.com");
		urlConnection = (HttpURLConnection)myUrl.openConnection();
		BufferedReader in = new BufferedReader (new InputStreamReader(urlConnection.getInputStream()));
		
		//int Counter=0;
		while(in.readLine()!=null)
		{
			line=line+in.readLine().toString();
			//Counter++;
		}
		
	//	myTxt.setText(line);
		}
		catch(Exception ex0)
		{
		Log.d("Error : ",ex0.toString());
	//	myTxt.setText(ex0.toString());
		}
		finally
		{
			if(urlConnection!=null){urlConnection.disconnect(); 	}
			
			
		}
		return line;
		
	}
	
	
	
	
	
	class RetrieveFeedTask extends AsyncTask<string,> {

	    

	    protected String doInBackground(String... urls) {
	    	try
	    	{
	        return ShowHtml();
	    	}
	    	catch(Exception e)
	    	{
	    	return e.toString();
	    	}
	    }

	    protected void onPostExecute(String Result) {
	    	TextView mytxt2=(TextView)findViewById(R.id.txtReturn);
	    	mytxt2.setText(Result);
	      
	    }
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		new RetrieveFeedTask().execute();
	}

	

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900