Java Question Cannot Find Symbol Error Pro

keirS

Well-Known Member
Licensed User
Longtime User
B4X:
Compiling code.                         0.08
Compiling layouts code.                 0.01
Generating R file.                      0.43
Compiling generated Java code.          Error
javac 1.6.0_26
src\b4a\example\main.java:225: cannot find symbol
symbol  : class requestHTTP
location: class b4a.example.main
public static requestHTTP _ohttp = null;
              ^
1 error

Can't figure out why I am getting this. Have searched the forum and tried rebuilding the Eclipse config from scratch as suggested but no joy. Occur when I do Dim oHttp As requestHTTP in B4A. My library code.

B4X:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.io.InputStreamReader;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.IterableList;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.objects.collections.Map;


@ShortName("requestHTTP") 
@Permissions(values = {"android.permission.INTERNET"}) 
@Author("KeirS") 
@Version(1) 
public class requestHTTP {

   
   /**
   * Simple one line Http Request
   *For GET requests pass null to the postData parameter
   *If you do not have headers pass null to the header parameter
   */

   public static String HttpsRequestUrl(String Url,String requestType, Map Header,String postData, String contentType ) throws IOException {
      HttpURLConnection reqConnection = null;
         OutputStreamWriter writeRequest = null;
         BufferedReader readResponse = null;
         StringBuilder sb = null;
         String line = null;
         URL serverAddress = null;
        
         try {
         serverAddress = new URL(Url);
      } catch (MalformedURLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
          
          
        
          
          try {
         reqConnection = (HttpURLConnection)serverAddress.openConnection();
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
       
          
        reqConnection.setAllowUserInteraction(false);         
        reqConnection.setDoOutput(true);
        reqConnection.setRequestProperty("Content-Type",contentType);
        
        
        try {
         reqConnection.setRequestMethod(requestType);
      } catch (ProtocolException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      
       
        if (Header != null) {
        IterableList addHeaders = Header.Keys();
        
       
       for (int nCounter = 0 ; nCounter < addHeaders.getSize(); nCounter++) {
          
          
         reqConnection.addRequestProperty(Header.GetKeyAt(nCounter).toString(),Header.GetValueAt(nCounter).toString());
           
       }
                  
       
        }
        
        reqConnection.connect();
      if (postData != null) {
        writeRequest  = new OutputStreamWriter(reqConnection.getOutputStream());
        writeRequest.write(postData);
        writeRequest.flush();
      }
      
      readResponse = new BufferedReader(new InputStreamReader(reqConnection.getInputStream()));
       sb = new StringBuilder();
     
       while ((line = readResponse.readLine()) != null)
       {
           sb.append(line + '\n');
       }
       reqConnection.disconnect();
       
       return sb.toString();
      
}
}


XML file

B4X:
<?xml version="1.0" encoding="UTF-8" ?> 
- <root>
  <doclet-version-NOT-library-version>1.02</doclet-version-NOT-library-version> 
- <class>
  <name>requestHTTP</name> 
  <shortname>requestHTTP</shortname> 
  <owner>process</owner> 
  <permission>android.permission.INTERNET</permission> 
- <method>
  <name>HttpsRequestUrl</name> 
  <comment>Simple one line Http Request For GET requests pass null to the postData parameter If you do not have headers pass null to the header parameter</comment> 
  <returntype>java.lang.String</returntype> 
- <parameter>
  <name>Url</name> 
  <type>java.lang.String</type> 
  </parameter>
- <parameter>
  <name>requestType</name> 
  <type>java.lang.String</type> 
  </parameter>
- <parameter>
  <name>Header</name> 
  <type>anywheresoftware.b4a.objects.collections.Map</type> 
  </parameter>
- <parameter>
  <name>postData</name> 
  <type>java.lang.String</type> 
  </parameter>
- <parameter>
  <name>contentType</name> 
  <type>java.lang.String</type> 
  </parameter>
  </method>
  </class>
  <version>1.0</version> 
  <author>KeirS</author> 
  </root>

Any help appreciated.
 

keirS

Well-Known Member
Licensed User
Longtime User
OK got this working. In Eclipse classes must be in a named package. Using the default package generates the above error. Why?
 

agraham

Expert
Licensed User
Longtime User
I think the answer is here in the error text.
symbol : class requestHTTP
location: class b4a.example.main
As the class has no package name it looks like the compiler expects it to belong to the package being compiled and so is looking for

B4X:
b4a.example.main.requestHTTP
 

keirS

Well-Known Member
Licensed User
Longtime User
Looks like that could be it. Got it all working in Java only to discover that you cant do network IO in the main process thread in Android :signOops: Now reading up on AsyncTask so I can wrap what I have in another class.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…