gpt4 book ai didi

Android - android.os.NetworkOnMainThreadException

转载 作者:IT王子 更新时间:2023-10-28 23:47:00 24 4
gpt4 key购买 nike

我有这个异常(exception),我正在阅读一个关于这个的线程,它看起来很困惑:

How to fix android.os.NetworkOnMainThreadException?

我已经将此行添加到我的 list 中:

<uses-permission android:name="android.permission.INTERNET" />

在那次讨论中,他们谈到了应用程序的主执行线程无法进行联网。我想知道的是如何重组我的代码,使其符合 Android 的良好实践。

这是我的 Activity 类:

package com.problemio;

import java.io.InputStream;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class LoginActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login);

// Show form for login_email
final EditText loginEmail = (EditText) findViewById(R.id.login_email);
String name = loginEmail.getText().toString();

// Show field for password
final EditText password = (EditText) findViewById(R.id.password);
String text = password.getText().toString();

// Show button for submit
Button submit = (Button)findViewById(R.id.submit);




// Show options for create-profile and forgot-password




submit.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
String email = loginEmail.getText().toString();
String pass = password.getText().toString();
sendFeedback(pass, email);
}
});
}


public void sendFeedback(String pass , String email)
{
Log.d( "1" , pass );
Log.d( "1" , email );

// Go to db and check if these r legit
// How do I do that? :)
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", email ));
postParameters.add(new BasicNameValuePair("password", pass ));

String responseString = null;

try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("myUrl");

// no idea what this does :)
httppost.setEntity(new UrlEncodedFormEntity(postParameters));

// This is the line that send the request
HttpResponse response = httpclient.execute(httppost);

HttpEntity entity = response.getEntity();

InputStream is = entity.getContent();
}
catch (Exception e)
{
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
}

我在这里做错了什么,我该如何解决? :) 谢谢!!

最佳答案

NetworkOnMainThreadException:当应用程序尝试在其主线程上执行网络操作时引发的异常。

您应该在 asynctask 上调用 sendfeedback 方法,然后只有上面的代码才能工作。由于网络服务器需要大量时间来响应主线程变得无响应。为避免它,您应该在另一个线程上调用它。因此 asynctask 更好。

这里是 link这说明了如何使用 asynctask

关于Android - android.os.NetworkOnMainThreadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9413625/

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