通过单击按钮

时间:2016-02-22 15:43:22

标签: android android-asynctask django-rest-framework onclicklistener django-authentication

我编辑了我之前的所有问题。我很久以前就解决了。 但现在我在这里,只是完成所有事情:

我有登录活动: 这会使用凭据来调用api。

我创建了它正常工作的登录活动,但我必须按两次按钮才能执行某些操作。我认为我的代码中存在错误:

这是我的登录活动...如果有人可以帮助我,我将接受它作为答案,并将关闭我的胶粘物上的这个链接....

login.java:

import android.app.Activity;

import android.app.ProgressDialog;

import android.content.Intent;

import android.os.AsyncTask;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;


import org.apache.http.HttpEntity;

import org.apache.http.NameValuePair;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.HttpResponse;

import org.apache.http.client.utils.URLEncodedUtils;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.util.EntityUtils;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.util.List;



public class login_act extends Activity {
private ProgressDialog pDialog;

List<NameValuePair> params=null;
static String response = null;

private String url = "http://hostname_ip/rest-api/xxxxx/?format=json";
static String u="";
static String p="";
String temp= "";
// User name
private EditText et_Username;
// Password
private EditText et_Password;
// Sign In
private Button bt_SignIn;
// Message
private TextView tv_Message;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_layout);


    // Initialization
    et_Username = (EditText) findViewById(R.id.u_name);
    et_Password = (EditText) findViewById(R.id.password);
    bt_SignIn = (Button) findViewById(R.id.sign_in);
    tv_Message = (TextView) findViewById(R.id.statusop);

    bt_SignIn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            // Stores User name
            String username = String.valueOf(et_Username.getText());
            u=username;

            // Stores Password
            String password = String.valueOf(et_Password.getText());
            p=password;

            new Getlogin().execute();

    }


    });


}


private class Getlogin extends AsyncTask<Void, Void, Void> {


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(login_act.this);
        pDialog.setMessage("Signing In...");
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {


        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

        String credentials = u + ":" + p;

        try {

                if (params != null) {
                    String paramString = URLEncodedUtils
                            .format(params, "utf-8");
                    url += "?" + paramString;
                }
                HttpGet httpGet = new HttpGet(url);

                String base64EncodedCredentials = Base64.encodeBytes(credentials.getBytes());
                httpGet.addHeader("Authorization", "Basic " + base64EncodedCredentials);

                httpResponse = httpClient.execute(httpGet);

                httpEntity = httpResponse.getEntity();
                temp = response = EntityUtils.toString(httpEntity);



        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;

    }


    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())

      try {
              pDialog.dismiss();
   if(temp.contains("count")){
              tv_Message.setText("Logged In");

              Intent go = new Intent(login_act.this,Scnd.class);


              Bundle extras = new Bundle();
              extras.putString("status", response);
              extras.putString("user", u);
              extras.putString("pass", p);

              // 4. add bundle to intent
              go.putExtras(extras);
              startActivity(go);

              finish();

          }else
              tv_Message.setText("Invalid username or password");


          }
          catch (Exception e)
          {
              e.printStackTrace();
          }



    }




}



}

我只想要用户和通行证是否正确然后意图进行新活动。如果用户密码错误,则显示带有incorect用户密码的响应

但是活动通过单击两次按钮来执行它。我想让它单击..任何帮助?我会很感激的。

1 个答案:

答案 0 :(得分:0)

您需要安装Newfies-Dialer才能使用API​​,换句话说,API网址将来自您自己的服务器。

文档清楚地说API URL =&gt; http://HOSTNAME_IP/rest-api/campaigns/

相关问题