ユーザー名とパスワードが正しい場合は、「成功」と表示される必要があります。それ以外の場合は「失敗」と表示されますBasicNameValuePair
。そして、NullPointerException
この行に表示されますint code = pres.getStatusLine().getStatusCode();
public class MyPostActivity extends Activity {
DefaultHttpClient client;
HttpPost post;
HttpResponse res;
HttpEntity ent;
Button b;
List<NameValuePair> pairs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b = (Button) findViewById(R.id.button1);
client = new DefaultHttpClient();
post = new HttpPost(
"http://somesite.com/abc");
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
List<NameValuePair> pairs = new ArrayList<NameValuePair>(3);
pairs.add(new BasicNameValuePair("Email", "avinash"));
pairs.add(new BasicNameValuePair("password", "avinash2"));
try {
post.setEntity(new UrlEncodedFormEntity(pairs));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpResponse pres = null;
try {
pres = client.execute(post);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int code = pres.getStatusLine().getStatusCode();
if (code == 200) {
Toast.makeText(getApplicationContext(), "Successful",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Failed!",
Toast.LENGTH_SHORT).show();
}
}
});
}
}