2015-01-02 33 views
0

這就是我基本上怎麼我的應用程序功能登錄一個HTTPS網頁。POST數據使用的HttpClient

  1. 這是一個登錄我的大學網站並查看學生詳細信息的應用程序。
  2. 用戶名和密碼將發佈到登錄網址。
  3. 成功登錄後,我將能夠在webView中查看學生的詳細信息。

我有一些問題,我的代碼。我插入了正確的用戶名和ID,但未能發佈數據。我嘗試了一些不同的方法,但仍然無法正常工作。我能知道爲什麼嗎?

package com.project; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 

import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 


import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.webkit.WebView; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class AndroidLogin extends Activity 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.main); 

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

     result = (TextView)findViewById(R.id.lbl_result); 

    } 

    public void postLoginData() { 
     // Create a new HttpClient and Post Header 
     HttpClient httpclient = new DefaultHttpClient(); 

     /* returns true if username and password is correct */ 
     HttpPost httppost = new HttpPost("https://icems.mmu.edu.my/sic/vlogin.jsp"); 

     try { 
      // Add user name and password 
      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(); 

      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
      nameValuePairs.add(new BasicNameValuePair("form_loginUsername", username)); 
      nameValuePairs.add(new BasicNameValuePair("login-password", password)); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      // Execute HTTP Post Request 
      Log.w("MMU", "Execute HTTP Post Request"); 
      HttpResponse response = httpclient.execute(httppost); 

      String str = inputStreamToString(response.getEntity().getContent()).toString(); 
      Log.w("MMU", str); 

      if(str.toString().equalsIgnoreCase("true")) 
      { 
       Log.w("MMU", "Login success"); 

      }else 
      { 
       Log.w("MMU", "Login fail"); 

      } 

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

    private StringBuilder inputStreamToString(InputStream is) { 
     String line = ""; 
     StringBuilder total = new StringBuilder(); 
     // Wrap a BufferedReader around the InputStream 
     BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
     // Read response until the end 
     try { 
      while ((line = rd.readLine()) != null) { 
       total.append(line); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     // Return full string 
     return total; 
    } 

    @Override 
    public void onClick(View view) { 
     if(view == ok){ 
      postLoginData(); 
      WebView AchievementWeb = (WebView) findViewById(R.id.webViewAchievement); 
      AchievementWeb.loadUrl("https://icems.mmu.edu.my/sic/vaas/vaas_main.jsp"); 
     } 
    } 

} 

這是logcat的

02 03:38:11.502: D/dalvikvm(800): GC_FOR_ALLOC freed 86K, 8% free 2934K/3180K, paused 130ms, total 133ms 
01-02 03:38:12.062: D/(800): HostConnection::get() New Host Connection established 0x2a207598, tid 800 
01-02 03:38:13.202: E/cutils-trace(800): Error opening trace file: No such file or directory (2) 
01-02 03:38:13.582: D/TilesManager(800): Starting TG #0, 0x2a2ea4c0 
01-02 03:38:39.863: D/InputEventConsistencyVerifier(800): KeyEvent: ACTION_UP but key was not down. 
01-02 03:38:39.863: D/InputEventConsistencyVerifier(800): in android.widget.EditText{417107b8 VFED..CL .F....I. 102,114-252,162 #7f050004 app:id/txt_password} 
01-02 03:38:39.863: D/InputEventConsistencyVerifier(800): 0: sent at 172536000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=172536, downTime=172448, deviceId=0, source=0x101 } 
01-02 03:38:43.723: W/MMU(800): Execute HTTP Post Request 
01-02 03:38:44.943: D/dalvikvm(800): GC_FOR_ALLOC freed 339K, 14% free 3108K/3608K, paused 39ms, total 42ms 
01-02 03:38:45.163: W/MMU(800): <HTML><HEAD><TITLE>500 Internal Server Error</TITLE></HEAD><BODY><H1>500 Internal Server Error</H1><PRE>java.lang.NullPointerException<br></PRE></BODY></HTML> 
01-02 03:38:45.163: W/MMU(800): Login fail 
01-02 03:38:46.123: E/chromium_net(800): external/chromium/net/disk_cache/block_files.cc:81: [0102/033846:ERROR:block_files.cc(81)] Failing CreateMapBlock 
01-02 03:38:46.132: E/chromium_net(800): external/chromium/net/disk_cache/entry_impl.cc:904: [0102/033846:ERROR:entry_impl.cc(904)] Failed to save user data 
01-02 03:38:46.483: E/chromium_net(800): external/chromium/net/disk_cache/rankings.cc:762: [0102/033846:ERROR:rankings.cc(762)] Inconsistent LRU. 
01-02 03:38:46.483: E/chromium_net(800): external/chromium/net/disk_cache/backend_impl.cc:1107: [0102/033846:ERROR:backend_impl.cc(1107)] Critical error found -8 
01-02 03:38:46.483: W/chromium_net(800): external/chromium/net/disk_cache/storage_block-inl.h:119: [0102/033846:WARNING:storage_block-inl.h(119)] Failed data load. 
01-02 03:38:46.513: W/chromium_net(800): external/chromium/net/disk_cache/storage_block-inl.h:119: [0102/033846:WARNING:storage_block-inl.h(119)] Failed data load. 
01-02 03:38:46.533: W/chromium_net(800): external/chromium/net/disk_cache/storage_block-inl.h:119: [0102/033846:WARNING:storage_block-inl.h(119)] Failed data load. 
01-02 03:38:46.553: D/chromium(800): Unknown chromium error: -401 
+0

'我嘗試了一些不同方法,但它仍然無法正常工作「你面臨什麼問題? –

+0

目前應用越來越對主UI線程從服務器數據,以便使用[的AsyncTask(http://developer.android.com/reference/android/os/AsyncTask.html) –

+0

登錄失敗,我無法發佈做在我的後臺將數據登錄到https網址。 –

回答

0

你應該張貼您的錯誤信息來解決你的問題。但我想你會得到一個「NetworkOnMainThreadException」,因爲你試圖在你的應用程序主線程上執行網絡操作。您可以使用異步任務。

您可以檢查此爲您解決問題。 How to fix android.os.NetworkOnMainThreadException?

如果你得到一個不同的錯誤,請張貼錯誤消息。

+0

我已經發布了錯誤消息。 –

+0

我已經發布了錯誤消息msg –

+0

你的api有問題。檢查你的名字值對和後端代碼。猜一猜,「login-password」可以是login_password「嗎? – savepopulation

0
HttpClient client = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost(
      "enterwebsite"); 

    httppost.setHeader("Content-Type", "application/x-www-form-urlencoded"); 
    // httppost.setHeader("Content-Type", 
    // "application/x-www-form-urlencoded"); 
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
    nameValuePairs.add(new BasicNameValuePair("oauth_uid", oauth_uid)); 
    nameValuePairs.add(new BasicNameValuePair("group_id", group_id)); 
    nameValuePairs.add(new BasicNameValuePair("user_id", user_id)); 

    HttpResponse mresponce; 

    try { 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); 
     mresponce = client.execute(httppost); 

     HttpEntity mentity = mresponce.getEntity(); 
     mstream = mentity.getContent(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       mstream, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line); 

     } 

     String json = sb.toString(); 

     Log.v("JOIN", json); 

     mList = new ArrayList<GetterSetteList>(); 
     Object obj = new JSONTokener(json).nextValue(); 
     if (obj instanceof JSONObject) { 
      JSONObject objs = new JSONObject(json); 

      messagex = (String) objs.opt("message"); 
     } 

    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     Log.v("ERROR", "API_JoinGroup" + e.toString()); 
     flag = 1; 
     e.printStackTrace(); 
     Log.v("JOIN", e.toString()); 
    } 
0

正如其他人所說,你應該從你貼我看到這個正在打印日誌換您的來電「postLoginData()」中的AsyncTask否則這個電話會給你一個NetworkOnMainThreadException。」不過,

> 01-02 03:38:45.163: W/MMU(800): <HTML><HEAD><TITLE>500 Internal Server 
> Error</TITLE></HEAD><BODY><H1>500 Internal Server 
> Error</H1><PRE>java.lang.NullPointerException<br></PRE></BODY></HTML> 

你確定你使用的是「https://icems.mmu.edu.my/sic/vlogin.jsp」和查詢PARAMS「form_loginUserName」和「登錄密碼」是否正確?網址 它與普通帖子的客戶端工作或其他地方?