使用Android从服务器获取数据并加载新屏幕

Ale*_*hew 4 android

我正在使用以下代码从服务器获取数据,如果来自服务器的值为,GEEK则它将加载至下一个类,但不会加载新视图。你能说出什么问题吗?

    公共无效onCreate(捆绑保存的InstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        登录=(按钮)findViewById(R.id.login);
        用户名=(EditText)findViewById(R.id.username);
        密码=(EditText)findViewById(R.id.password);

        login.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v){
字符串Re;
                字符串mUsername = username.getText()。toString();
                字符串mPassword = password.getText()。toString();

                Re = tryLogin(mUsername,mPassword);

                if(Re ==“ GEEK”)
                {
                    意图i = new Intent();
                    i.setClassName(v.getContext(),“ com.httplogin.MainScreen”);
                    startActivity(i);

                }
            }
        });
    }

    受保护的String tryLogin(String mUsername,String mPassword)
    {           
        HttpURLConnection连接;
       OutputStreamWriter request = null;

            URL url = null;   
            字符串响应= null;         
            字符串参数=“ username =” + mUsername +“&password =” + mPassword;   

            尝试
            {
                url =新的URL(“ http://serverspace/script.php”);
                连接=(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(参数);
                request.flush();
                request.close();            
                字符串行=“”;               
                InputStreamReader isr =新的InputStreamReader(connection.getInputStream());
                BufferedReader reader =新的BufferedReader(isr);
                StringBuilder sb = new StringBuilder();
                while((line = reader.readLine())!= null)
                {
                    sb.append(line +“ \ n”);
                }
                //登录过程后服务器的响应将存储在响应变量中。                
                响应= sb.toString();

                isr.close();
                reader.close();


            }
            catch(IOException e)
            {
                Toast.makeText(this,e.toString(),0).show();
            }
            返回响应;
    }
}

San*_*esh 5

请参阅此代码。..............,使用修整功能,因为它消除了字符串中的空格。

public class HttpLogin extends Activity {
    /** Called when the activity is first created. */
    private Button login;
    private EditText username, password;

    @Override
    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);
                Log.d(" Check ","Here");
                Log.d("Re",Re);
                String temp_check=Re.trim();
                if(temp_check.equals("GEEK"))
                {
                    Intent i = new Intent();
                    i.setClassName(v.getContext(),"com.httplogin.MainScreen");
                    startActivity(i);

                }
                else
                {
                 //Toast.makeText(HttpLogin.this,"MAX Returned",0).show();
                 displayAlert(Re);
//                  Intent i = new Intent();
//                     i.setClassName(v.getContext(),"com.san.MainScreen");
//                     startActivity(i);
                }
            }
        });
    }

    protected String tryLogin(String mUsername, String mPassword)
    {           
      Log.d(" TryLoginCheck ","Here");
        HttpURLConnection connection;
       OutputStreamWriter request = null;

            URL url = null;   
            String response = null;   
            String temp=null;
            String parameters = "username="+mUsername+"&password="+mPassword;   
            System.out.println("UserName"+mUsername+"\n"+"password"+mPassword);
            Log.d("Parameters",parameters);
            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");
                }
                temp=sb.toString();
                Log.d("Temp",temp);
                // Response from server after login process will be stored in response variable.                
                response = sb.toString();
                Log.d("Response",response);
               Log.d("Sb Value",sb.toString());
                isr.close();
                reader.close();


            }
            catch(IOException e)
            {
                Toast.makeText(this,e.toString(),0).show();
            }
           // Log.d("Response",response);
            return response;
    }
    public  void displayAlert(String Re)
    {
     new AlertDialog.Builder(this).setMessage(Re)
        .setTitle("Returned Value")  
        .setCancelable(true)  
        .setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() {  
           public void onClick(DialogInterface dialog, int whichButton){
            finish();
           }  
           })  
        .show(); 
    }
}
Run Code Online (Sandbox Code Playgroud)