2014-10-06 57 views
0

我尝试了一个登录页面上的adroid,并不断收到错误408,我确认我的页面存在于服务器上。以下是我的代码。Android登录页面出现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(); 

     } 

LoginTask.java

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

以下是我AndoirdManifest.xml,我已经给Internet权限。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.guard1" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="21" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

我在我的logcat中捕获了一些错误。

10-06 01:11:54.969: W/System.err(1801): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
10-06 01:11:54.999: W/System.err(1801):  at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6094) 
10-06 01:11:54.999: W/System.err(1801):  at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:857) 
10-06 01:11:54.999: W/System.err(1801):  at android.view.ViewGroup.invalidateChild(ViewGroup.java:4320) 
10-06 01:11:54.999: W/System.err(1801):  at android.view.View.invalidate(View.java:10935) 
10-06 01:11:54.999: W/System.err(1801):  at android.view.View.invalidate(View.java:10890) 
10-06 01:11:55.099: W/System.err(1801):  at android.widget.TextView.checkForRelayout(TextView.java:6587) 
10-06 01:11:55.099: W/System.err(1801):  at android.widget.TextView.setText(TextView.java:3813) 
10-06 01:11:55.109: W/System.err(1801):  at android.widget.TextView.setText(TextView.java:3671) 
10-06 01:11:55.189: W/System.err(1801):  at android.widget.TextView.setText(TextView.java:3646) 
10-06 01:11:55.189: W/System.err(1801):  at android.app.ProgressDialog.setMessage(ProgressDialog.java:316) 
10-06 01:11:55.189: W/System.err(1801):  at com.example.guard1.LoginTask.doInBackground(LoginTask.java:56) 
10-06 01:11:55.199: W/System.err(1801):  at com.example.guard1.LoginTask.doInBackground(LoginTask.java:1) 
10-06 01:11:55.229: W/System.err(1801):  at android.os.AsyncTask$2.call(AsyncTask.java:288) 
10-06 01:11:55.229: W/System.err(1801):  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
10-06 01:11:55.239: W/System.err(1801):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
10-06 01:11:55.239: W/System.err(1801):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
10-06 01:11:55.239: W/System.err(1801):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
10-06 01:11:55.249: W/System.err(1801):  at java.lang.Thread.run(Thread.java:841) 
+0

尝试在一个run内烘烤onOiThread – 2014-10-06 06:06:32

+0

哪个函数应该在你的意思是doInBackground? – user3953386 2014-10-06 06:10:50

+0

我认为你的错误不是因为408,因为UI线程,所以请尝试runOnUiThread内的activity.login(id)。 – 2014-10-06 06:16:25

回答

0

在烤面包和任何其他UI更改中尝试此操作。

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

         } 
        }); 

     } 
+0

我试过了,看到这个在我的logcat 10-06 02:29:01.919:I /编舞(1956):跳过了58帧!应用程序可能在其主线程上做了太多工作。 10-06 02:29:02.279:I/Choreographer(1956):跳过了84帧!应用程序可能在其主线程上做了太多工作。 最后的结果仍然指向误差函数 – user3953386 2014-10-06 06:29:59

+0

好吧,我做了改变,你给了,并将此敬酒,而((行= rd.readLine())!= NULL) \t \t \t { \t \t \t \t结果=行。修剪(); \t \t \t \t Log.w(“SENCIDE RSULT”,result); \t \t \t}确认它打印出正确的结果。所以现在是通过的问题,我想成为 – user3953386 2014-10-06 07:14:57

+0

我也注意到响应代码实际上是200而不是202 – user3953386 2014-10-06 07:20:15