Click here to Skip to main content
15,034,432 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My android code is giving a lot of these errors " Error: <identifier> expected " or" Error: ';' expected " even though everything seems to be fine. Please help me if anyone knows why this might be happening. Thank you in advance.



customHttpClient.java
Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.impl.client.DefaultHttpClient;



public class CustomHttpClient {
    public static final int HTTP_TIMEOUT= 30*1000;
    private static HttpClient mHttpClient;
    private static HttpClient getHttpClient(){
        if (mHttpClient == null) {
            mHttpClient = new DefaultHttpClient();
            final HttpParams params = mHttpClient.getParams();
            HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
            HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
            ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
        }
        return mHttpClient;
    }

    public static String executeHttpPost(String url,ArrayList<NameValuePair>throws Exception){  //1 error in this line.
        BufferedReader in= null;
        try{ //1 error in this line.
            HttpClient client= getHttpClient();
            HttpPost request= new HttpPost(url); //1 error in this line.
            UrlEncodedFormEntity formEntity= new UrlEncodedFormEntity(postParameters);//1 error in this line.
            request.setEntity(formEntity); //1 error in this line. 
            HttpResponse response= client.execute(request); //1 error in this line.
            in= new BufferedReader(new InputStreamReader(response.getEntity().getContent())); //1 error in this line.
            StringBuffer sb= new StringBuffer(""); //1 error in this line.
            String line=""; //1 error in this line.
            String NL= System.getProperty("line.separator"); //1 error in this line.
            while((line=in.readLine())!= null){  //1 error in this line.
                sb.append(line+NL);
            } //1 error in this line.
            in.close();
            String result= sb.toString(); //1 error in this line.
            return result; //1 error in this line.

        }finally{ //1 error in this line.
            if(in!= null){
                try{
                    in.close();
                }catch(IOException e){ //1 error in this line.
                    e.printStackTrace();
                }//1 error in this line.
            }
        }
    }

    public static String executeHttpGet(String url) throws Exception{ //1 error in this line.
        BufferedReader in= null;
        try{ //1 error in this line.
            HttpClient client= getHttpClient();
            HttpGet request= new HttpGet(); //1 error in this line.
            request.setURI(new URI(url)); //1 error in this line.
        } //1 error in this line.
    }


}


The errors above is " error. class, interface, or enum expected "



LogIn.java
Java
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.logging.ErrorManager;

import static android.R.attr.id;
import static android.R.id.content;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;



public class LogIn extends Activity {

    Button b;
    EditText t1,t2;
    TextView error;

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

        b=(Button)findViewById(R.id.button);
        t1=(EditText)findViewById(R.id.editText);
        t2=(EditText)findViewById(R.id.editText2);
        error=(TextView)findViewById(R.id.textView6);

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                postParameters.add(new BasicNameValuePair("email", t1.getText().toString()));
                postParameters.add(new BasicNameValuePair("password", t2.getText().toString()));

                String response = null;
                try {
                    response = CustomHttpClient.executeHttpPost("<target page url>", postParameters);
                    String res = response.toString();
                    res = res.replaceAll("\\s+", "");
                    if (res.equals(1))
                        error.setText("Correct Credentials.");
                    else
                        error.setText("Invalid Credentials.");
                } catch (Exception ex) {
                    t1.setText(ex.toString());
                }
            }
        });
    }// Error in this line.


The error above is " error: reached end of file while parsing "



MainActivity.java
Java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.os.Handler;

public class MainActivity extends AppCompatActivity {

    private static int SPLASH_TIME=3000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        int secondsDelayed = 3;
        new Handler().postDelayed(new Runnable() {
            public void run() {
                startActivity(new Intent(MainActivity.this, LogIn.class));
                finish();
            }
        } secondsDelayed * 1000); //4 errors here.
    }
}

The errors above are " error ')' expected "
" error. ';' expected "
" error. illegal start of type "

What I have tried:

I have tried Rewriting the code.
Posted
Updated 15-Dec-16 18:02pm
v9
Comments
CPallini 13-Dec-16 2:27am
   
You should shouw the offending line of code as well.
Member 12901284 15-Dec-16 1:36am
   
I'm sorry I did not do it before. Ive dont it now. Please have a look.
Richard MacCutchan 13-Dec-16 4:20am
   
If everything was fine then it would not be giving you the errors.
Member 12901284 15-Dec-16 1:48am
   
Thats obvious :p
I meant everything "looks" good..
Please have a look at the code.. Thank you.
Richard MacCutchan 15-Dec-16 4:12am
   
I have had a look and cannot see anything obvious. so, the chances are there is something deeper causing the problems, maybe one of the imported files causes the problem. Unfortunately this is something you are going to have to diagnose for yourself because there is just too much to wade through to find where the cause starts.
Member 12901284 15-Dec-16 23:31pm
   
Thank you for taking your time and looking into the code. I'll check the imported files. Thank You.

1 solution

Quote:
Please help me if anyone knows why this might be happening.
Yes you have errors in your code, but without see that code, we can not help.

For real help, show the code and add comment in code to show us position of errors. include error messages in comments.

[Update]
Check if you didn't forgot a } in line before this one.
Java
public static String executeHttpPost(String url,ArrayList<NameValuePair>throws Exception){  //5 errors in this line.

Note: this error generate other errors. correct this and update question.
   
v3
Comments
Member 12901284 13-Dec-16 23:53pm
   
I wanted to do that in the first place but I dont know how to attach screenshots here..
Patrice T 14-Dec-16 0:15am
   
Source code is TEXT.
insert comments in your code saying "error this in next line"
and paste your code, select the code and use code button to have it formatted.
Use Improve question to update your question.
Member 12901284 15-Dec-16 0:56am
   
Done as you said.. Please see and reply.. Thank you
Patrice T 15-Dec-16 1:04am
   
And what happened ?
Does it solve some errors ?
Member 12901284 15-Dec-16 1:24am
   
I did that and It did solve a lot of errors but now I have these errors. Also please look into other java class files also. They have errors too. Thank you.
Jochen Arndt 15-Dec-16 9:17am
   
Just look at your code:

} secondsDelayed * 1000); //4 errors here.

There is a closing parenthesis but not an openening one.
While 'secondsDelayed * 1000' is syntactical possible it makes no sense (the result is not used) and is probably not what you want to achieve.
Member 12901284 15-Dec-16 23:30pm
   
The opening parenthesis is there in the above class.. But I thought so..
my previous code was as given below... but it wasnt working so I just coppied that code above.. pls tell me if I can use the below code for the splash screen.


Thread timerThread= new Thread();
public void run(){
try{
sleep(3000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent intent = new Intent(SplashScreen.this,MainActivity.class);
startActivity(intent);
}
}
};
timerThread.start();

@Override //To end activity so it wont come back to this act if pressed back
protected void onPause() {
super.onPause();
finish();
}
Jochen Arndt 16-Dec-16 2:58am
   
Not the '}' parentheses. The ')' parentheses.
Patrice T 16-Dec-16 5:46am
   
Sorry, I am not a Java user, I can(t help you on this.
Open a new question.

I was able to find your problem because the unmatched braces is an easy problem shared with C/C++
Member 12901284 15-Dec-16 23:43pm
   
Also could You please look into the "LogIn.java" class file.. I feel there is a Logic related error there.. Thank You
Jochen Arndt 16-Dec-16 3:03am
   
I don't know Java.

But even without knowing Java there are obvious syntax errors.
You should try to solve those yourself. The compiler tells you about them and solving them yourself is part of the learning process for a programming language.

If you ask here to get all your errors be solved, you will learn nothing.

If you got stuck with a single error, raise a new question posting only the relevant code part.
Member 12901284 16-Dec-16 4:22am
   
As far as I think see that the parenthesis is correct. And I wanted to know about the logic related part of the code. Also, I'll keep in mind about your advice. Thank you for taking your time and replying.

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