Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android登录页面显示408错误,但确认页面存在_Java_Android - Fatal编程技术网

Java Android登录页面显示408错误,但确认页面存在

Java Android登录页面显示408错误,但确认页面存在,java,android,Java,Android,我正在adroid上尝试登录页面,并不断出现错误408,我已确认服务器上存在我的页面。以下是我的代码 Mainactivity.java public class MainActivity extends ActionBarActivity implements OnClickListener { Button ok,back,exit; TextView result; /** Called when the activity is first cr

我正在adroid上尝试登录页面,并不断出现错误408,我已确认服务器上存在我的页面。以下是我的代码

Mainactivity.java

 public class MainActivity extends ActionBarActivity implements OnClickListener   {

    Button ok,back,exit;
     TextView result;

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

            // Login button clicked
            ok = (Button)findViewById(R.id.btnLogin);
            ok.setOnClickListener(this);

           // result = (TextView)findViewById(R.id.lbl_result);
            //ok.setOnClickListener(loginOnClickListener);  
        }

        public void onClick(View view) {
              if(view == ok){
                  EditText uname = (EditText)findViewById(R.id.txt_username);
                    String username = uname.getText().toString();

                    EditText pword = (EditText)findViewById(R.id.txt_password);
                    String password = pword.getText().toString();
                    Log.w("SENCIDE",username );
                    Log.w("SENCIDE",password );
                    ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
                    progressDialog.setMessage("Logging in...");
                    progressDialog.setCancelable(false);

                    LoginTask loginTask = new LoginTask(MainActivity.this, progressDialog);
                    loginTask.execute(username,password);
              }
            }
        /*protected OnClickListener loginOnClickListener = new OnClickListener()
        {
            public void onClick(View v) 
            {

            }
        };*/

        public void showLoginError(int result)
        {

            Log.w("SENCIDE",Integer.toString(result) );
            Toast.makeText(getApplicationContext(), "ERROR",Toast.LENGTH_LONG).show();

        }

        // do some stuff after user logs in
        public void login(int id)
        {
            Toast.makeText(getApplicationContext(), "OK LOGGED IN",Toast.LENGTH_LONG).show();

        }
public class LoginTask extends AsyncTask<String, Void, Integer> {

private ProgressDialog progressDialog;
private MainActivity activity;
private int id = -1;

public LoginTask(MainActivity activity, ProgressDialog progressDialog)
{
    this.activity = activity;
    this.progressDialog = progressDialog;
}

@Override
protected void onPreExecute()
{
    progressDialog.show();
}

@Override
protected Integer doInBackground(String... arg0) 
{
    String result = "";
    int responseCode = 0;
    try 
    {
        HttpClient client = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(" http://*****/***/login.php");

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("username", arg0[0]));
            nameValuePairs.add(new BasicNameValuePair("password", arg0[1]));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        int executeCount = 0;
        HttpResponse response;
        do
        {
            progressDialog.setMessage("Logging in.. ("+(executeCount+1)+"/5)");
            // Execute HTTP Post Request
            executeCount++;
            response = client.execute(httppost);
            responseCode = response.getStatusLine().getStatusCode();                        
            // If you want to see the response code, you can Log it
            // out here by calling:
            // Log.d("256 Design", "statusCode: " + responseCode)
        } while (executeCount < 5 && responseCode == 408);

        BufferedReader rd = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent()));

        String line;
        while ((line = rd.readLine()) != null)
        {
            result = line.trim();
        }
        id = Integer.parseInt(result);
    }
    catch (Exception e) {
        responseCode = 408;
        e.printStackTrace();
    }
    return responseCode;
}

@Override
protected void onPostExecute(Integer headerCode)
{
    progressDialog.dismiss();
    if(headerCode == 202)
        activity.login(id);
    else{
        activity.showLoginError(headerCode);
    }
}
LoginTask.java

 public class MainActivity extends ActionBarActivity implements OnClickListener   {

    Button ok,back,exit;
     TextView result;

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

            // Login button clicked
            ok = (Button)findViewById(R.id.btnLogin);
            ok.setOnClickListener(this);

           // result = (TextView)findViewById(R.id.lbl_result);
            //ok.setOnClickListener(loginOnClickListener);  
        }

        public void onClick(View view) {
              if(view == ok){
                  EditText uname = (EditText)findViewById(R.id.txt_username);
                    String username = uname.getText().toString();

                    EditText pword = (EditText)findViewById(R.id.txt_password);
                    String password = pword.getText().toString();
                    Log.w("SENCIDE",username );
                    Log.w("SENCIDE",password );
                    ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
                    progressDialog.setMessage("Logging in...");
                    progressDialog.setCancelable(false);

                    LoginTask loginTask = new LoginTask(MainActivity.this, progressDialog);
                    loginTask.execute(username,password);
              }
            }
        /*protected OnClickListener loginOnClickListener = new OnClickListener()
        {
            public void onClick(View v) 
            {

            }
        };*/

        public void showLoginError(int result)
        {

            Log.w("SENCIDE",Integer.toString(result) );
            Toast.makeText(getApplicationContext(), "ERROR",Toast.LENGTH_LONG).show();

        }

        // do some stuff after user logs in
        public void login(int id)
        {
            Toast.makeText(getApplicationContext(), "OK LOGGED IN",Toast.LENGTH_LONG).show();

        }
public class LoginTask extends AsyncTask<String, Void, Integer> {

private ProgressDialog progressDialog;
private MainActivity activity;
private int id = -1;

public LoginTask(MainActivity activity, ProgressDialog progressDialog)
{
    this.activity = activity;
    this.progressDialog = progressDialog;
}

@Override
protected void onPreExecute()
{
    progressDialog.show();
}

@Override
protected Integer doInBackground(String... arg0) 
{
    String result = "";
    int responseCode = 0;
    try 
    {
        HttpClient client = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(" http://*****/***/login.php");

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("username", arg0[0]));
            nameValuePairs.add(new BasicNameValuePair("password", arg0[1]));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        int executeCount = 0;
        HttpResponse response;
        do
        {
            progressDialog.setMessage("Logging in.. ("+(executeCount+1)+"/5)");
            // Execute HTTP Post Request
            executeCount++;
            response = client.execute(httppost);
            responseCode = response.getStatusLine().getStatusCode();                        
            // If you want to see the response code, you can Log it
            // out here by calling:
            // Log.d("256 Design", "statusCode: " + responseCode)
        } while (executeCount < 5 && responseCode == 408);

        BufferedReader rd = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent()));

        String line;
        while ((line = rd.readLine()) != null)
        {
            result = line.trim();
        }
        id = Integer.parseInt(result);
    }
    catch (Exception e) {
        responseCode = 408;
        e.printStackTrace();
    }
    return responseCode;
}

@Override
protected void onPostExecute(Integer headerCode)
{
    progressDialog.dismiss();
    if(headerCode == 202)
        activity.login(id);
    else{
        activity.showLoginError(headerCode);
    }
}

在toast和任何其他UI更改中尝试此操作

public void login(int id)
        {
runOnUiThread(new Runnable() {
                        public void run() {
                            Toast.makeText(getApplicationContext(), "OK LOGGED IN",Toast.LENGTH_LONG).show();

                        }
                    });

        }

尝试RunnuiThread中的toast我应该在哪个函数中toast?你是说doInBackground?我想你的错误不是因为408,而是因为UI线程,所以请尝试RunnuiThread中的activity.login(id)。很抱歉,我在这里有点不知所措,该activity.login(id)放在哪里?我是个新手,那么UI线程是什么呢?我也是android新手,请检查我的答案,这可能会对你有所帮助,这只是猜测。我试过并在我的logcat 10-06 02:29:01.919:I/Choreographer(1956):跳过了58帧!应用程序可能在其主线程上做了太多工作。10-06 02:29:02.279:I/编舞(1956):跳过84帧!应用程序可能在其主线程上做了太多工作。最后的结果仍然指向错误函数OK我做了您所做的更改,并在((line=rd.readLine())!=null){result=line.trim();Log.w(“SENCIDE RSULT”,result);}确认它打印了正确的结果。所以现在是通过的问题,我想我也注意到响应代码实际上是200而不是2022。当你说UI相关的代码时,我非常困惑。为了和大家分享,我现在做了一些修改,如果(headerCode==200){activity.login(id);},它工作得非常好。所以问题在于响应代码。无需运行runOnUIThread