Results 1 to 3 of 3
- 06-04-2012, 12:14 PM #1
Member
- Join Date
- May 2012
- Posts
- 12
- Rep Power
- 0
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(); } } }
- 06-04-2012, 01:13 PM #2
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 06-04-2012, 01:44 PM #3
Member
- Join Date
- May 2012
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
non-static variable cannot be referenced from static context...
By MadJack in forum New To JavaReplies: 5Last Post: 12-01-2010, 06:43 AM -
non-static variable cannot be referenced from a static context
By keo in forum New To JavaReplies: 5Last Post: 10-15-2010, 04:21 AM -
non-static variable grade cannot be referenced from a static context
By pictianpravin in forum New To JavaReplies: 3Last Post: 02-11-2010, 09:59 AM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks