2013-03-21 71 views
0

任何人都可以纠正我这个Java代码?我是一个初学者在Java编程和Android开发,我迷失在这些大括号,所以任何人都可以帮我纠正这个代码? :/ P,S我使用日食 ,我试图创建连接到我的本地数据库 一个登录界面,但问题是,Eclipse是给我的错误在括号 我觉得有什么缺失 希望有人能帮我android数据库登录表格

public void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.user); 

    initialise(); 
} 

private void initialise() { 
    // TODO Auto-generated method stub 
    etUser = (EditText) findViewById(R.id.editText2); 
    etPass = (EditText) findViewById(R.id.editText1); 
    bLogin = (Button) findViewById(R.id.button1); 
    bLogin.setOnClickListener(this); 
} 


public void onregister(final View button) { 
    final Intent intent = new Intent(); 
    intent.setClass(this, register.class); 
startActivity(intent); 

} 


public void onClick(final View v) { 

httpclient = new DefaultHttpClient(); 
httppost = new HttpPost("http://10.0.2.2/blood_needed/database.php"); 

username = etUser.getText().toString(); 
password = etPass.getText().toString(); 


try{ 
    nameValuePairs = new ArrayList<NameValuePair>() ; 


    nameValuePairs.add(new BasicNameValuePair("username" , username)); 
    nameValuePairs.add(new BasicNameValuePair("password", password)); 

    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

    final Thread a = new Thread(new Runnable() { 


     public void run() { 
    response = httpclient.execute(httppost); 

    runOnUiThread(new Runnable(){ 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      if(response.getStatusLine().getStatusCode()== 200){ 

       entity = response.getEntity(); 

       if(entity != null) { 

        InputStream instream = entity.getContent(); 

        JSONObject jsonResponse = new JSONObject(convertStreamToString(instream)); 
        String retUser = jsonResponse.getString("username"); 
        String retPass = jsonResponse.getString("password"); 

        if(username.equals(retUser) && password.equals(retPass)){ 

         SharedPreferences sp = getSharedPreferences("logindetails", 0); 

         SharedPreferences.Editor spedit = sp.edit(); 

         spedit.putString("username", username); 
         spedit.putString("password", password); 

         spedit.commit(); 

         Toast.makeText(getBaseContext(), "Succes!", Toast.LENGTH_SHORT).show(); 


        } else { 

         Toast.makeText(getBaseContext(), "Invalid Login Details", Toast.LENGTH_SHORT).show(); 


        } 
     } 

    } 




     } 


    } 

      ); 

catch(Exception e) { 


    Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_SHORT).show(); 

} 



    a.start(); 
private static String convertStreamToString(final InputStream is) { 

final BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
final StringBuilder sb = new StringBuilder(); 

String line = null; 
try { 
    while ((line = reader.readLine()) != null) { 
     sb.append(line + "\n"); 
    } 
} catch (final IOException e) { 
    e.printStackTrace(); 
} finally { 
    try { 
     is.close(); 
    } catch (final IOException e) { 
     e.printStackTrace(); 
    } 
} 
return sb.toString(); 

}

}

+0

你可以发布logcat吗? – staaar 2013-03-21 14:27:59

+0

@staaar:我认为问题在于Charbel并没有关闭所有的大括号:) – 2013-03-21 14:32:48

+0

有时重新启动Eclipse可以解决奇怪的问题......我必须承认,我没有足够的勇气潜入大括号中查看是否有问题。 – dqms 2013-03-21 14:39:36

回答

1

请即行后面插入4个括号:

a.start(); 
}}}} 

,你应该能够编译...

- 更新 - 使用此:

public void onCreate(final Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.user); 

initialise(); 
} 

private void initialise() { 
    // TODO Auto-generated method stub 
    etUser = (EditText) findViewById(R.id.editText2); 
    etPass = (EditText) findViewById(R.id.editText1); 
    bLogin = (Button) findViewById(R.id.button1); 
    bLogin.setOnClickListener(this); 
} 

public void onregister(final View button) { 
final Intent intent = new Intent(); 
intent.setClass(this, register.class); 
startActivity(intent); 

} 

public void onClick(final View v) { 

httpclient = new DefaultHttpClient(); 
httppost = new HttpPost("http://10.0.2.2/blood_needed/database.php"); 

username = etUser.getText().toString(); 
password = etPass.getText().toString(); 

try { 
    nameValuePairs = new ArrayList<NameValuePair>(); 

    nameValuePairs.add(new BasicNameValuePair("username", username)); 
    nameValuePairs.add(new BasicNameValuePair("password", password)); 

    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

    final Thread a = new Thread(new Runnable() { 

    public void run() { 
     response = httpclient.execute(httppost); 

     runOnUiThread(new Runnable() { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      if (response.getStatusLine().getStatusCode() == 200) { 

      entity = response.getEntity(); 

      if (entity != null) { 

       InputStream instream = entity.getContent(); 

       JSONObject jsonResponse = new JSONObject(convertStreamToString(instream)); 
       String retUser = jsonResponse.getString("username"); 
       String retPass = jsonResponse.getString("password"); 

       if (username.equals(retUser) && password.equals(retPass)) { 

       SharedPreferences sp = getSharedPreferences("logindetails", 0); 

       SharedPreferences.Editor spedit = sp.edit(); 

       spedit.putString("username", username); 
       spedit.putString("password", password); 

       spedit.commit(); 

       Toast.makeText(getBaseContext(), "Succes!", Toast.LENGTH_SHORT).show(); 

       } else { 

       Toast.makeText(getBaseContext(), "Invalid Login Details", Toast.LENGTH_SHORT).show(); 

       } 
      } 
      } 
     } 
     }); 
    } 
    }); 
    a.start(); 
} catch (Exception e) { 

    Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_SHORT).show(); 

} 

} 

private static String convertStreamToString(final InputStream is) { 

final BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
final StringBuilder sb = new StringBuilder(); 

String line = null; 
try { 
    while ((line = reader.readLine()) != null) { 
    sb.append(line + "\n"); 
    } 
} catch (final IOException e) { 
    e.printStackTrace(); 
} finally { 
    try { 
    is.close(); 
    } catch (final IOException e) { 
    e.printStackTrace(); 
    } 
} 
return sb.toString(); 
} 

把该代码的类括号内。

+0

斯特凡,我仍然只有两个错误 – Charbel 2013-03-21 14:46:49

+0

一个在该行:); catch(Exception e){ – Charbel 2013-03-21 14:47:32

+0

其他的:a.start(); – Charbel 2013-03-21 14:47:58