Results 1 to 3 of 3
  1. #1
    chopficaro is offline Member
    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Default non static variable from static context error

    in this class i have a constructor and a main() function for testing the constructor
    theres a couple things stopping me from testing it in the command prompt tho
    the post data is a string with an ampersand in it and when i try to pass it to the main function in the command prompt it thinks that the ampersand means something, i dont know how to "escape" it
    also, i would like to know how to pass a value from the constructor to main() so i can print string test, i tried putting it outside both meathods into the class but it gave me non static context error
    Java Code:
    //takes am address, and post data, and returns the html as a string
    import java.net.*;
    import java.io.*;
    import java.lang.*;
    public class UrlToString
    {
    	public String test = "";
    	public static void main (String[] args) throws Exception
    	{
    		//enter url and post data in cmd prompt to test
    		new UrlToString  (test,args[0],args[1]);
    		System.out.println(test);
    	}
    	//paramaters of returned string, address to get it from, and post data in th for of variable1=value1&variable2=value2...
    	UrlToString (String returnString, String inAddr, String rawData) throws Exception
    	{
    		returnString = "";
    		//connection variables
    		String agent = "Mozilla/4.0";
    		String type = "application/x-www-form-urlencoded";
    		HttpURLConnection connection = null;
    		String encodedData = URLEncoder.encode( rawData, "UTF-8");
    		//connect
    		try 
    		{
    			URL searchUrl = new URL(inAddr);
    			connection = (HttpURLConnection)searchUrl.openConnection();
    			connection.setDoOutput(true);
    			connection.setRequestMethod("POST");
    			connection.setRequestProperty( "User-Agent", agent );
    			connection.setRequestProperty( "Content-Type", type );
    			connection.setRequestProperty( "Content-Length", Integer.toString(encodedData.length()) );
    			OutputStream os = connection.getOutputStream();
    			os.write( encodedData.getBytes() );
    			os.flush();
    			os.close();
    			//check if theres an http error
    			int rc = connection.getResponseCode();
    			if(rc==200)
    			{
    				//no http response code error
    				//read the result from the server
    				
    				InputStreamReader in = new InputStreamReader(connection.getInputStream());
    				BufferedReader br = new BufferedReader(in);
    				String strLine;
    				//Read File Line By Line
    				while ((strLine = br.readLine()) != null)
    				{
    					returnString = returnString.concat(strLine);
    					System.out.println(strLine);
    				}
    				return;
    			}
    			else
    			{
    				System.out.println("http response code error: "+rc+"\n");
    				return;
    			}
    
    		}
    		catch( IOException e )
    		{
    			System.out.println("search URL connect failed: " + e.getMessage());
    			e.printStackTrace();
    		}
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: non static variable from static context error

    Your stated problem is with regard to an ampersand in a command line parameter, and using the command line parameters in the construction of an instance of a class. So why did you post about 50 lines of code relating to making a network connection?

    To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    chopficaro is offline Member
    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Default Re: non static variable from static context error

    Quote Originally Posted by DarrylBurke View Post
    Your stated problem is with regard to an ampersand in a command line parameter, and using the command line parameters in the construction of an instance of a class. So why did you post about 50 lines of code relating to making a network connection?

    To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.

    db
    tldr
    lol
    but seriously tho the problem is in the first 5 lines of code

Similar Threads

  1. Replies: 5
    Last Post: 12-01-2010, 06:43 AM
  2. Replies: 5
    Last Post: 10-15-2010, 04:21 AM
  3. Replies: 3
    Last Post: 02-11-2010, 09:59 AM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •