2013-10-11 59 views
-2

这是我的登录应用程序连接到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 

我该如何解决这个问题?

+0

你在这里做了什么样的编码?你已经在'onClick'里面编写了完整的代码。你在代码中的某个地方缺少'}'。 – GrIsHu

+0

@Nor atika Salleh在'finally'附近删除'}'。 – Ram

+0

@Nor atika Salleh你在行尾添加了额外的'}',并且在'finally'块中移除'''''代码块的错误末尾。看看我的答案。 – GrIsHu

回答

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 {} 

        } 
       }); 
      } 
     }); 
    } 
} 
+0

感谢您的帮助。但是我得到这个错误: 没有类型LoginApplicationActivity的封闭实例可以在范围 –

+0

中访问,而不是'LoginApplicationActivity.this'写入'Main.this'。 – GrIsHu

+0

好的谢谢。它的作品 –

0
`finally {});`  

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

+0

我已经删除。但再次出现一些错误 –

+0

@Nor atika Salleh重新启动您的IDE。 – Ram

+0

对不起,还是一样。 –

相关问题