這就是我基本上怎麼我的應用程序功能登錄一個HTTPS網頁。POST數據使用的HttpClient
- 這是一個登錄我的大學網站並查看學生詳細信息的應用程序。
- 用戶名和密碼將發佈到登錄網址。
- 成功登錄後,我將能夠在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
'我嘗試了一些不同方法,但它仍然無法正常工作「你面臨什麼問題? –
目前應用越來越對主UI線程從服務器數據,以便使用[的AsyncTask(http://developer.android.com/reference/android/os/AsyncTask.html) –
登錄失敗,我無法發佈做在我的後臺將數據登錄到https網址。 –