如果用戶名爲&密碼正確,它必須顯示「SUCCESS」,否則必須顯示「FAILED」。我正在使用BasicNameValuePair
連接到服務器。其顯示NullPointerException
在這條線int code = pres.getStatusLine().getStatusCode();
簡單HttpPost上的NullPointerException
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();
}
}
});
}
}
認沽Web服務文件還 – 2013-03-26 11:18:07
你的意思是鏈接? – DroidLearner 2013-03-26 11:18:54
請分享logcat ...檢查您是否在撥打'執行'除了'執行' – 2013-03-26 11:19:08