【发布时间】:2013-03-16 04:01:18
【问题描述】:
如果用户名和密码正确,则必须显示“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();
}
}
});
}
}
【问题讨论】:
-
把网络服务文件也放
-
请分享 logcat... 检查您是否在致电
execute时收到任何异常 -
您的网络服务代码
-
你有上网权限吗?
-
向我们展示您的清单文件..您需要在清单中添加互联网权限才能从您的应用程序访问互联网..您也不应该在 UI 线程上进行网络调用使用
AsyncTask
标签: android http-post httprequest getjson httpexception