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

我使用以下代码从服务器获取数据,如果服务器的值是GEEK,那么它将加载到下一个类,但新视图不加载.你能说出什么问题吗?

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;

}

}

解决方法:

请参考此代码…………..,使用trim函数,因为它消除了String中的空间.

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

}

}

标签:android

来源: https://codeday.me/bug/20190730/1580438.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值