如果用户名和密码正确,则必须显示“SUCCESS”,否则必须显示“FAILED”。我正在使用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();
}
}
});
}
}