Hitesh Sofat Hitesh Sofat - 16 days ago 5
Android Question

Android httpClient

I am hitesh my problem is ,i work on httpclient in andriod i gave internet permission to my application and i run my application and click on button the tehy gave unfortunately stopped. please help me there my code

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText user=(EditText) findViewById(R.id.username);
final EditText pass=(EditText) findViewById(R.id.password);
Button login=(Button) findViewhere`ById(R.id.login);
login.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
String u=user.getText().toString();
String p=pass.getText().toString();
HttpClient httpClient=new DefaultHttpClient();
HttpPost httppost=new HttpPost("http://202.164.53.122");
BasicNameValuePair usernameBaseNameValuePair=new BasicNameValuePair("id",u);
BasicNameValuePair passBaseNameValuePair=new BasicNameValuePair("pass",p);
List<NameValuePair> nameValuePairList=new ArrayList<NameValuePair>();
nameValuePairList.add(usernameBaseNameValuePair);
nameValuePairList.add(passBaseNameValuePair);
try {
UrlEncodedFormEntity urlEncodedFormEntity=new
UrlEncodedFormEntity(nameValuePairList,HTTP.UTF_8);
httppost.setEntity(urlEncodedFormEntity);
try {
HttpResponse httpResponse=httpClient.execute(httppost);
} catch (ClientProtocolException e) {
} catch (IOException e) {
user.setText(e.getMessage());
}

}catch(UnsupportedEncodingException uee){
}
}
});
}

@Override

public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

Answer

dont work with network on main thread(or get android.os.NetworkOnMainThreadException). use thread class or AsyncTask class.
AsyncTask
Thread