0

请帮助我使用此代码创建登录页面。在匹配 url 值用户名和密码后进行身份验证

和JAVA代码,

public class AndroidHTTPRequestsActivity extends Activity implements OnClickListener {

    private EditText usernameEditText;
    private EditText passwordEditText;
    private Button sendGetReqButton;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        usernameEditText = (EditText) findViewById(R.id.main_username_editText);
        passwordEditText = (EditText) findViewById(R.id.main_password_editText);

        sendGetReqButton = (Button) findViewById(R.id.main_sendGetReq_button);
        sendGetReqButton.setOnClickListener(this);
    }

    public void onClick(View v) {

        if(v.getId() == R.id.main_sendGetReq_button) {
            // Get the values given in EditText fields
            String givenUsername = usernameEditText.getText().toString();
            String givenPassword = passwordEditText.getText().toString();
            System.out.println("Given usernames is :" + givenUsername + " Given password is :" + givenPassword);

            // Pass those values to connectWithHttpGet() method
            connectWithHttpGet(givenUsername, givenPassword);
        }        
    }

    private void connectWithHttpGet(String givenUsername, String givenPassword) {

        class HttpGetAsyncTask extends AsyncTask<String, Void, String> {

            @Override
            protected String doInBackground(String... params) {

                String email = params[0];
                String pwd = params[1];
                System.out.println("email is :" + email + " pwd is :" + pwd);


                HttpClient httpClient = new DefaultHttpClient();


                HttpGet httpGet = new HttpGet("http://ipadress/folder/filename.php?command=CMD_LOGIN&email=name&pwd=00");

                try {
                    HttpResponse httpResponse = httpClient.execute(httpGet);
                    System.out.println("httpResponse");
                    // that comes as the httpResponse
                    InputStream inputStream = httpResponse.getEntity().getContent();

                    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

                    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                    StringBuilder stringBuilder = new StringBuilder();
                    String bufferedStrChunk = null;

                    while((bufferedStrChunk = bufferedReader.readLine()) != null){
                        stringBuilder.append(bufferedStrChunk);
                    }
                    System.out.println("Returning value of doInBackground :" + stringBuilder.toString());

                    return stringBuilder.toString();
                } catch (ClientProtocolException cpe) {
                    System.out.println("Exception generates caz of httpResponse :" + cpe);
                    cpe.printStackTrace();
                } catch (IOException ioe) {
                    System.out.println("Second exception generates caz of httpResponse :" + ioe);
                    ioe.printStackTrace();
                }

                return null;
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);

                if(result.equals("1"))
                {

                    Toast.makeText(getApplicationContext(), "HTTP GET is working...", Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(getApplicationContext(), "Invalid...", Toast.LENGTH_LONG).show();
                }
            }
        }

        HttpGetAsyncTask httpGetAsyncTask = new HttpGetAsyncTask();

        httpGetAsyncTask.execute(givenUsername, givenPassword); 
    }
}

并像这样在 logcat 中获得响应。如何匹配结果值以显示登录成功

if(result.equal(""))

我应该与结果比较什么?我试过了value==1,它不起作用。

07-19 13:54:26.509: I/System.out(13951): Given usernames is :name Given password is :00
07-19 13:54:26.519: I/System.out(13951): email is :name pwd is :00
07-19 13:54:27.689: I/System.out(13951): httpResponse
07-19 13:54:27.689: I/System.out(13951): Returning value of doInBackground :<?xml version="1.0" encoding="ISO-8859-1"?><command><value>1</value><email>praveen@imean.local</email><id>100001</id><first_name>Sue</first_name><last_name>Stappler</last_name><photo>images/teacher.png</photo><total_students>2</total_students><student_list>   <student>       <id>200001</id>     <first_name>John</first_name>       <last_name>Mathew</last_name>       <photo>images/student.png</photo>   </student>  <student>       <id>200002</id>     <first_name>Steve</first_name>      <last_name>Jobs</last_name>     <photo>images/student.png</photo>   </student></student_list><description_title>Login Success</description_title><description_message>Correct username and password</description_message><session_id>ecaf065b3213179afcd0d9895691505c</session_id></command>
4

1 回答 1

0

您可以检查此请求的两种状态

  • HTTP 响应代码
    • 检查数据是否以“HTTP 200 OK”返回
  • 数据检查
    • 反序列化响应(SimpleXML、Jackson Json 或 FasterXML 项目)
    • 检查反序列化状态(如果模型更改或响应为空,则可能会失败)
    • 将反序列化对象中的字段与您的条件匹配
于 2013-07-19T09:58:23.003 回答