gpt4 book ai didi

android - 应用程式尝试登入网页时当机

转载 作者:行者123 更新时间:2023-12-03 17:45:43 25 4
gpt4 key购买 nike

我正在尝试登录网页,看看登录是否成功,但是我的应用程序崩溃了。这是我的代码:

package com.shortey.logtoweb;

import java.io.IOException;
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.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.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void login (View view) {
try {
final String LOGIN_LINK = "https://www.manodienynas.lt/";
final String MY_EMAIL = "********"; //here should be my username
final String MY_PW = "********"; //here should be my password

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

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("username", MY_EMAIL));
nameValuePairs.add(new BasicNameValuePair("password", MY_PW));
nameValuePairs.add(new BasicNameValuePair("submit_login", "Prisijungti"));

httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse response = httpClient.execute(httpPost);

TextView TV1 = (TextView)findViewById(R.id.textView1);

if (response.getStatusLine().getStatusCode() < 400) {
TV1.setText("Success");
} else {
TV1.setText("Failed");
}
} catch (UnsupportedEncodingException e) {

} catch (ClientProtocolException e) {

} catch (IOException e) {

}
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

我通过按一个按钮来调用方法login()。请帮助我修复导致我的应用崩溃的代码错误。我知道我没有正确处理异常,但是我想这不会导致我的应用崩溃,但也许可以帮助发现问题。在我的情况下如何正确处理它们?提前致谢!

最佳答案

您正在ui线程上运行与网络相关的操作。使用ThreadAsycTask

HttpResponse response = httpClient.execute(httpPost); // must be executed in a thread

关于android - 应用程式尝试登入网页时当机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22179445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com