0

谁能纠正我这个java代码?我是 java 编程和 android 开发的初学者,我迷失在这些大括号中,所以任何人都可以帮助我更正这段代码吗?:/ P,S我正在使用eclipse,我正在尝试创建一个连接到我的本地数据库的登录界面,但问题是eclipse在大括号上给了我错误我认为缺少一些东西希望有人可以帮助我

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();

}

}

4

1 回答 1

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();
}

将该代码放在您的类大括号中。

于 2013-03-21T14:42:51.277 回答