为什么此登录代码在编译时出现语法错误?

时间:2013-10-11 04:44:41

标签: android sql-server

这是我的登录应用程序连接到SQL Server的代码。

package com.helpdesk.loginapplication;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
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.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends Activity
{
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb=null;
public static final String LOG_TAG="HI..";
EditText un,pw;
TextView error;
Button ok;
int flag=0;
public static String name;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
un=(EditText)findViewById(R.id.et_un);
pw=(EditText)findViewById(R.id.et_pw);
ok=(Button)findViewById(R.id.btn_login);
error=(TextView)findViewById(R.id.tv_error);
ok.setOnClickListener(new View.OnClickListener()
{
public void onClick(final View v)
{
// create a new thread for httppost request
new Thread()
{
public void run()
{
ArrayList<namevaluepair> nameValuePairs = new ArrayList<namevaluepair> ();
//http post
try
{
String nm=un.getText().toString();
String pwd=pw.getText().toString();
HttpClient httpclient = new DefaultHttpClient();
//provide the values to asp.net script through query string
String qs="http://192.168.1.162/LoginApp/LoginApp/Default.aspx?userName="+nm+"&password="+pwd;
HttpPost httppost = new HttpPost(qs);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e)
{ Log.e("log_tag", "Error in http connection"+e.toString()); }
//convert response to string
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null){
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try
{
jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++)
{
flag=1;
json_data = jArray.getJSONObject(i);
name=json_data.getString("name");
String pwd=json_data.getString("password");
// get values from database and show on logcat view
//for invalid user, the values are null
Log.v(LOG_TAG,"name :"+name);
Log.v(LOG_TAG,"password :"+pwd);
}
if(flag==0)
{
//create new thread derived from “runOnUiThread” class to connect with View
// bcz simple Thread class can’t access View
LoginApplicationActivity.this.runOnUiThread(new Runnable()
{
public void run() {
error.setText("Invalid User...");
//Toast.makeText(LoginApplicationActivity.this, "Invalid
//Login,"Toast.LENGTH_LONG).show();
}
});
}
else{
Log.v("LOG-TAG","valid");
LoginApplicationActivity.this.runOnUiThread(new Runnable() {
public void run() {
error.setText("Login successful....");
// Toast.makeText(LoginApplicationActivity.this, "Login Successful",
//Toast.LENGTH_LONG).show();
//for valid user, start another activity “ULogin” which says ‘hello’ to user..
Intent myIntent = new Intent(v.getContext(), Ulogin.class);
startActivityForResult(myIntent, 0);
}

});
}
}
catch(JSONException e1){

Toast.makeText(getBaseContext(), "error" ,Toast.LENGTH_LONG).show();
}catch (ParseException e1) {
e1.printStackTrace();

}finally {}); 
}
}

此代码在“finally”行产生错误。

Syntax error, insert "}" to complete ClassBody  Main.java line 138 Java Problem
Syntax error, insert "}" to complete MethodBody Main.java line 138 Java Problem
Syntax error, insert ";" to complete Statement  Main.java line 138 Java Problem
Syntax error, insert "}" to complete ClassBody  Main.java line 138 Java Problem
Syntax error, insert "}" to complete MethodBody Main.java line 138 Java Problem

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

`finally {});`      

冗余“)”字符。删除它并再次运行代码。

答案 1 :(得分:0)

试试以下代码: 并始终确保关闭开始{括号和}括号。

package com.example.test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
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.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity {
    JSONArray jArray;
    String result = null;
    InputStream is = null;
    StringBuilder sb = null;
    public static final String LOG_TAG = "HI..";
    EditText un, pw;
    TextView error;
    Button ok;
    int flag = 0;

    // public static String name;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // un=(EditText)findViewById(R.id.et_un);
        // pw=(EditText)findViewById(R.id.et_pw);
        // ok=(Button)findViewById(R.id.btn_login);
        // error=(TextView)findViewById(R.id.tv_error);
        ok.setOnClickListener(new View.OnClickListener() {
            public void onClick(final View v) {
                // create a new thread for httppost request
                /*
                 * new Thread() { public void run() {
                 */
                Thread m = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        ArrayList<namevaluepair> nameValuePairs = new ArrayList<namevaluepair>();
                        // http post
                        try {
                            String nm = un.getText().toString();
                            String pwd = pw.getText().toString();
                            HttpClient httpclient = new DefaultHttpClient();
                            // provide the values to asp.net script through
                            // query string
                            String qs = "http://192.168.1.162/LoginApp/LoginApp/Default.aspx?userName="
                                    + nm + "&password=" + pwd;
                            HttpPost httppost = new HttpPost(qs);
                            httppost.setEntity(new UrlEncodedFormEntity(
                                    nameValuePairs));
                            HttpResponse response = httpclient
                                    .execute(httppost);
                            HttpEntity entity = response.getEntity();
                            is = entity.getContent();
                        } catch (Exception e) {
                            Log.e("log_tag",
                                    "Error in http connection" + e.toString());
                        }
                        // convert response to string
                        try {
                            BufferedReader reader = new BufferedReader(
                                    new InputStreamReader(is, "iso-8859-1"), 8);
                            sb = new StringBuilder();
                            sb.append(reader.readLine() + "\n");
                            String line = "0";
                            while ((line = reader.readLine()) != null) {
                                sb.append(line + "\n");
                            }
                            is.close();
                            result = sb.toString();
                        } catch (Exception e) {
                            Log.e("log_tag",
                                    "Error converting result " + e.toString());
                        }
                        try {
                            jArray = new JSONArray(result);
                            JSONObject json_data = null;
                            for (int i = 0; i < jArray.length(); i++) {
                                flag = 1;
                                json_data = jArray.getJSONObject(i);
                                name = json_data.getString("name");
                                String pwd = json_data.getString("password");
                                // get values from database and show on logcat
                                // view
                                // for invalid user, the values are null
                                Log.v(LOG_TAG, "name :" + name);
                                Log.v(LOG_TAG, "password :" + pwd);
                            }
                            if (flag == 0) {
                                // create new thread derived from
                                // “runOnUiThread” class to connect with View
                                // bcz simple Thread class can’t access View
                                LoginApplicationActivity.this
                                        .runOnUiThread(new Runnable() {
                                            public void run() {
                                                error.setText("Invalid User...");
                                                // Toast.makeText(LoginApplicationActivity.this,
                                                // "Invalid
                                                // Login,"Toast.LENGTH_LONG).show();
                                            }
                                        });
                            } else {
                                Log.v("LOG-TAG", "valid");
                                LoginApplicationActivity.this
                                        .runOnUiThread(new Runnable() {
                                            public void run() {
                                                error.setText("Login successful....");
                                                // Toast.makeText(LoginApplicationActivity.this,
                                                // "Login Successful",
                                                // Toast.LENGTH_LONG).show();
                                                // for valid user, start another
                                                // activity “ULogin” which says
                                                // ‘hello’ to user..
                                                Intent myIntent = new Intent(v
                                                        .getContext(),
                                                        Ulogin.class);
                                                startActivityForResult(
                                                        myIntent, 0);
                                            }
                                        });
                            }
                        } catch (JSONException e1) {
                            Toast.makeText(getBaseContext(), "error",
                                    Toast.LENGTH_LONG).show();
                        }finally {}

                    }
                });
            }
        });
    }
}