错误>>网络主线程异常 [英] error>> network on main thread exception
本文介绍了错误>>网络主线程异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
package com.example.bms;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
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.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Add_Customer extends 活动{
EditText customerName,addr1,addr2,person1,mob,pos,note;
按钮保存,取消;
List< namevaluepair> namevaluepairs中;
InputStream是;
DateFormat dateFormat = new SimpleDateFormat( dd /月/年跨度>);
日历cal = Calendar.getInstance();
@覆盖
受保护 void onCreate(Bundle savedInstanceState){
// TODO自动生成的方法存根
super .onCreate(savedInstanceState);
setContentView(R.layout.add_customer);
customerName =(EditText)findViewById(R.id.CustomerName);
addr1 =(EditText)findViewById(R.id.addr1);
保存=(按钮)findViewById(R.id.save);
cancel =(Button)findViewById(R.id.cancel);
addr2 =(EditText)findViewById(R.id.addr2);
person1 =(EditText)findViewById(R.id.Person);
mob =(EditText)findViewById(R.id.Mobile);
pos =(EditText)findViewById(R.id.Position);
note =(EditText)findViewById(R.id.tNote);
Save.setOnClickListener( new OnClickListener(){
@覆盖
public void onClick(查看v){
< span class =code-keyword> try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost( http://123.12。 11.1 / insertCustomer.php);
nameValuePairs = new ArrayList< namevaluepair>( 3 );
nameValuePairs.add( new BasicNameValuePair( cName,customerName.getText()。toString()));
nameValuePa irs.add( new BasicNameValuePair( addr1,addr1.getText()。toString()));
nameValuePairs.add( new BasicNameValuePair( addr2,addr2.getText()。toString()));
nameValuePairs.add( new BasicNameValuePair( person1,person1.getText()。toString()));
nameValuePairs.add( new BasicNameValuePair( mob,mob.getText()。toString()));
nameValuePairs.add( new BasicNameValuePair( pos,pos.getText()。toString()));
nameValuePairs.add( new BasicNameValuePair( 注意,note.getText()。toString()));
nameValuePairs.add( new BasicNameValuePair( date,dateFormat.format(cal.getTime())));
UrlEncodedFormEntity URLEntity = new UrlEncodedFormEntity(nameValuePairs);
httppost.setEntity(URLEntity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
是= entity.getContent();
int status = response.getStatusLine()。getStatusCode();
if (status == 200)
Toast.makeText(Add_Customer。 this , 添加了新客户信息,成功!,Toast.LENGTH_LONG)。show();
else Toast.makeText(Add_Customer。 this , 未保存客户信息,http连接错误:,Toast.LENGTH_LONG)。show();
} catch (例外e){
Toast.makeText(Add_Customer。此, http连接错误: + e.toString() ,Toast.LENGTH_LONG)。show();
}
}
});
}
}
这可以解决主线程异常网络的错误..我如何解决!??
Thnnks表示。
Vishal
解决方案
这是因为Android的更高版本不允许在主线程中访问网络: a href =http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html> http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html [< a href =http://developer.android.com/reference/android/os/NetworkOnMainThreadException.htmltarget =_ blanktitle =New Window> ^ ]。
要解决此问题,请使用AsyncTask或Thread来执行任务。以下是使用AsyncTask的示例: http://stackoverflow.com/questions/6343166/android-os- networkonmainthreadexception [ ^ ]
感谢朋友,但我得到了解决方案......它没有互联网许可!!!
package com.example.bms;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
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.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Add_Customer extends Activity{
EditText customerName,addr1,addr2,person1,mob,pos,note;
Button Save,cancel;
List<namevaluepair> nameValuePairs;
InputStream is;
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal = Calendar.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_customer);
customerName=(EditText)findViewById(R.id.CustomerName);
addr1=(EditText)findViewById(R.id.addr1);
Save=(Button)findViewById(R.id.save);
cancel=(Button)findViewById(R.id.cancel);
addr2=(EditText)findViewById(R.id.addr2);
person1=(EditText)findViewById(R.id.Person);
mob=(EditText)findViewById(R.id.Mobile);
pos=(EditText)findViewById(R.id.Position);
note=(EditText)findViewById(R.id.tNote);
Save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://123.12.11.1/insertCustomer.php");
nameValuePairs = new ArrayList<namevaluepair>(3);
nameValuePairs.add(new BasicNameValuePair("cName", customerName.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("addr1", addr1.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("addr2", addr2.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("person1", person1.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("mob", mob.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("pos", pos.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("note", note.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("date", dateFormat.format(cal.getTime())));
UrlEncodedFormEntity URLEntity = new UrlEncodedFormEntity(nameValuePairs);
httppost.setEntity(URLEntity );
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
int status = response.getStatusLine().getStatusCode();
if(status==200)
Toast.makeText(Add_Customer.this,"New Customer Information added,Successfully !", Toast.LENGTH_LONG).show();
else Toast.makeText(Add_Customer.this,"Customer Information Not Saved, Error in http Connection: ", Toast.LENGTH_LONG).show();
}catch(Exception e){
Toast.makeText(Add_Customer.this,"Error in http Connection: "+e.toString(), Toast.LENGTH_LONG).show();
}
}
});
}
}
this gots the error of network on main thread exception.. how i resolve!??
Thnnks regards.
Vishal
解决方案
This is because of later version of Android does not allow accessing to the network in main thread: http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html[^].
To solve this problem, use AsyncTask or Thread to perform your task. Here''s an example of using AsyncTask: http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception[^]
thanks friends but i got solution... its not have an internet permission!!!
这篇关于错误>>网络主线程异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文