다음 코드를 사용하여 서버에서 데이터를 가져오고 서버의 값이 GEEK
이면 다음 클래스로로드되지만 새보기가로드되지 않습니다. 문제가 무엇인지 말할 수 있습니까?서버에서 데이터 가져 오기 및 Android를 사용하여 새 화면로드
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); login = (Button) findViewById(R.id.login); username = (EditText) findViewById(R.id.username); password = (EditText) findViewById(R.id.password); login.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String Re; String mUsername = username.getText().toString(); String mPassword = password.getText().toString(); Re=tryLogin(mUsername, mPassword); if(Re=="GEEK") { Intent i = new Intent(); i.setClassName(v.getContext(),"com.httplogin.MainScreen"); startActivity(i); } } }); } protected String tryLogin(String mUsername, String mPassword) { HttpURLConnection connection; OutputStreamWriter request = null; URL url = null; String response = null; String parameters = "username="+mUsername+"&password="+mPassword; try { url = new URL("http://serverspace/script.php"); connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestMethod("POST"); request = new OutputStreamWriter(connection.getOutputStream()); request.write(parameters); request.flush(); request.close(); String line = ""; InputStreamReader isr = new InputStreamReader(connection.getInputStream()); BufferedReader reader = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } // Response from server after login process will be stored in response variable. response = sb.toString(); isr.close(); reader.close(); } catch(IOException e) { Toast.makeText(this,e.toString(),0).show(); } return response; } }
코드 스 니펫을 추가 할 수 있습니까 ?? –
if (Re.equals ("GEEK")) {...} 자세히 알아보기 Here-> http://developer.android.com/reference/java/lang/String.html – ninjasense