2012-11-12 76 views
3

我必須登錄到在ASP做了一個網站一個問題... 這裏是我使用的代碼:Java的Android的HTTP POST登錄錯誤

public class MainActivity extends Activity { 
Button bottone; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     bottone = (Button) findViewById(R.id.button1); 
     if (android.os.Build.VERSION.SDK_INT > 9) { 
      StrictMode.ThreadPolicy policy = 
        new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
      StrictMode.setThreadPolicy(policy); 
      } 
     bottone.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // starting new Async Task 
       try { 
        login(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 
    public static void login() throws IOException 
    { 
     String loginurl = "https://www.blurum.it/Web/", user, pass; 
     user = "tester55"; 
     pass = "provapassword321"; 

     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(loginurl); 

     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3); 
     nameValuePairs.add(new BasicNameValuePair("ctrl_LoginControl$loginView$c01$RPLogin$LoginView$Username", user)); 
     nameValuePairs.add(new BasicNameValuePair("ctrl_LoginControl$loginView$c01$RPLogin$LoginView$Password", pass)); 
     nameValuePairs.add(new BasicNameValuePair("remember", "on")); 
     nameValuePairs.add(new BasicNameValuePair("ctrl_LoginControl$loginView$c01$RPLogin$LoginView$LoginButton", "Accedi")); 



     httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     // Execute HTTP Post Request 
     HttpResponse response = httpClient.execute(httpPost); 
     Log.d("logggd", "response stat code " + response.getStatusLine().getStatusCode()); 

     if (response.getStatusLine().getStatusCode() < 400) { 

      String cookie = response.getFirstHeader("Cookie") 
        .getValue(); 
      Log.d("logggdc", "cookie: " + cookie); 

      // get the contacts page 
      HttpGet getContacts = new HttpGet("https://www.blurum.it/Web/default.aspx"); 
      getContacts.setHeader("Cookie", cookie); 
      response = httpClient.execute(getContacts); 

      InputStream ins = response.getEntity().getContent(); 
      BufferedReader in = new BufferedReader(new InputStreamReader(
        ins)); 

      String inputLine; 
      while ((inputLine = in.readLine()) != null) { 
       Log.d("logggd1", " " + inputLine); 
      } 

      in.close(); 
     } else { 
       Log.d("logggd2", "Response error: " 
         + response.getStatusLine().getStatusCode()); 
      } 
     } 
    } 

但我的應用程序崩潰,所以我不能在代碼中找出錯誤...這裏是logcat的:

11-12 20:05:37.678: W/dalvikvm(1806): threadid=1: thread exiting with uncaught exception (group=0xb4177180) 
11-12 20:05:37.688: E/AndroidRuntime(1806): FATAL EXCEPTION: main 
11-12 20:05:37.688: E/AndroidRuntime(1806): java.lang.NullPointerException 
11-12 20:05:37.688: E/AndroidRuntime(1806):  at com.example.testlogin.MainActivity.login(MainActivity.java:93) 
11-12 20:05:37.688: E/AndroidRuntime(1806):  at com.example.testlogin.MainActivity$1.onClick(MainActivity.java:53) 
11-12 20:05:37.688: E/AndroidRuntime(1806):  at android.view.View.performClick(View.java:3511) 
11-12 20:05:37.688: E/AndroidRuntime(1806):  at android.view.View$PerformClick.run(View.java:14105) 
11-12 20:05:37.688: E/AndroidRuntime(1806):  at android.os.Handler.handleCallback(Handler.java:605) 
11-12 20:05:37.688: E/AndroidRuntime(1806):  at android.os.Handler.dispatchMessage(Handler.java:92) 
11-12 20:05:37.688: E/AndroidRuntime(1806):  at android.os.Looper.loop(Looper.java:137) 
11-12 20:05:37.688: E/AndroidRuntime(1806):  at android.app.ActivityThread.main(ActivityThread.java:4424) 
11-12 20:05:37.688: E/AndroidRuntime(1806):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-12 20:05:37.688: E/AndroidRuntime(1806):  at java.lang.reflect.Method.invoke(Method.java:511) 
11-12 20:05:37.688: E/AndroidRuntime(1806):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
11-12 20:05:37.688: E/AndroidRuntime(1806):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
11-12 20:05:37.688: E/AndroidRuntime(1806):  at dalvik.system.NativeStart.main(Native Method) 
11-12 20:05:38.218: I/dalvikvm(1806): threadid=3: reacting to signal 3 
11-12 20:05:38.228: I/dalvikvm(1806): Wrote stack traces to '/data/anr/traces.txt' 

我提取的數據發佈到服務器在Firefox具有防篡改數據,這裏是一個屏幕,登錄我想在2職位劃分,這裏是屏幕: http://i.stack.imgur.com/GZiZ0.png http ://i.stack.imgur.com/FKH3P.png

我不確定是否必須發送第一頁或第二個發佈數據,可以是這個錯誤嗎?謝謝。

+0

什麼是MainActivity類中的第93行? – kosa

+0

92-93行是這樣的:String cookie = response.getFirstHeader(「Cookie」) .getValue(); – user1818263

回答

1

看來response.getFirstHeader("Cookie")正在返回null

作爲每javadoc

返回與此消息的指定名稱的第一報頭。標題值被忽略。如果消息中有多個匹配頭,則返回getHeaders(String)的第一個元素。如果在消息沒有空匹配的頭返回

解決方案:

String cookie = null; 
if(response.getFirstHeader("Cookie") != null) 
{ 
cookie = response.getFirstHeader("Cookie") 
        .getValue(); 
} 
1

不能撥打:

調用getValue();

實例之前做一個null檢查在你的代碼塊中不止一次下面的行;

response.getStatusLine() 

你在你的行中多次調用它,看起來像這樣;

Log.d("logggd", "response stat code " + response.getStatusLine().getStatusCode()); 

然後你在這一行再次調用它;

if (response.getStatusLine().getStatusCode() < 400) { 

註釋掉response.getStatusLine()第一通話 - 這就是你要嘗試登錄的狀態代碼。所以你的代碼應該看起來像這樣;

//Log.d("logggd", "response stat code " + response.getStatusLine().getStatusCode()); 

您還想嘗試在您的方法login()的某個位置執行此操作;

// Execute HTTP Post Request 
    HttpResponse response = httpClient.execute(httpPost); 
    StatusLine statusLine = response.getStatusLine(); 

    Log.d("logggd", "response stat code " + statusLine.getStatusCode()); 


    if (statusLine.getStatusCode() < 400) { 

     String cookie = null; 
      if(response.getFirstHeader("Cookie") != null){ 
       cookie = response.getFirstHeader("Cookie").getValue(); 
      } 
      Log.d("logggdc", "cookie: " + cookie); 

    //...etc....etc...etc