嗨,我是安卓世界的新手。我正在尝试为我的应用程序创建一个登录页面,该页面将通过 editext 获取用户 ID 和密码并将其发布在 url 中以进行登录身份验证。我已经编写了以下代码(来源互联网),但我的应用程序崩溃了。我还想检查身份验证是否成功,但由于我的应用程序崩溃,我无法更进一步。有人可以帮我吗。下面是我的应用程序的代码。谢谢。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
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.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Login extends Activity {
Button ok,back,exit;
TextView result;
String tag= "abc";
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void postLoginData(String username, String password ) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("(my url)");
try {
// Add user name and password
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.i(tag, "name and password");
// Execute HTTP Post Request
Log.w(tag, "Execute HTTP Post Request");
HttpResponse response = httpclient.execute(httppost);
String str = InputStreamToString(response.getEntity().getContent()).toString();
} catch (UnsupportedEncodingException 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;
}
LOGCAT:
01-02 09:13:14.802: W/dalvikvm(2933): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
01-02 09:13:14.951: E/AndroidRuntime(2933): FATAL EXCEPTION: main
01-02 09:13:14.951: E/AndroidRuntime(2933): java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
01-02 09:13:14.951: E/AndroidRuntime(2933): at com.example.project1.MainActivity$1.onClick(MainActivity.java:40)
01-02 09:13:14.951: E/AndroidRuntime(2933): at android.view.View.performClick(View.java:4202)
01-02 09:13:14.951: E/AndroidRuntime(2933): at android.view.View$PerformClick.run(View.java:17340)
01-02 09:13:14.951: E/AndroidRuntime(2933): at android.os.Handler.handleCallback(Handler.java:725)
01-02 09:13:14.951: E/AndroidRuntime(2933): at android.os.Handler.dispatchMessage(Handler.java:92)
01-02 09:13:14.951: E/AndroidRuntime(2933): at android.os.Looper.loop(Looper.java:137)
01-02 09:13:14.951: E/AndroidRuntime(2933): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-02 09:13:14.951: E/AndroidRuntime(2933): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 09:13:14.951: E/AndroidRuntime(2933): at java.lang.reflect.Method.invoke(Method.java:511)
01-02 09:13:14.951: E/AndroidRuntime(2933): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-02 09:13:14.951: E/AndroidRuntime(2933): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-02 09:13:14.951: E/AndroidRuntime(2933): at dalvik.system.NativeStart.main(Native Method)
01-02 09:13:15.191: W/ActivityManager(278): Force finishing activity com.example.project1/.MainActivity
This is MainActivity:
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
Button ok,back,exit;
String tag= "Project 1";
/** Called when the activity is first created. */
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Login button clicked
ok = (Button)findViewById(R.id.b);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText uname = (EditText)findViewById(R.id.id);
String username = uname.getText().toString();
EditText pword = (EditText)findViewById(R.id.psswd);
String password = pword.getText().toString();
Login l = new Login();
l.postLoginData(username, password);
}
});
}
}