Reputation: 2135
If username & Password are correct it has to show "SUCCESS" else it has to show "FAILED".I'm connecting to a server using BasicNameValuePair
. and its showing NullPointerException
on this line 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();
}
}
});
}
}
Upvotes: 2
Views: 1069
Reputation: 13805
Please add this permission also to your manifest and try
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
And AsyncTask code
private class SignInService extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
try {
progressDilaog = ProgressDialog.show(MainActivity.this, "",
"Loading", true, false);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected Void doInBackground(Void... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("username", "abc"));
nameValuePairs.add(new BasicNameValuePair("password", "bcd"));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
try {
HttpResponse response = httpclient.execute(httppost);
int responsecode = response.getStatusLine().getStatusCode();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progressDilaog.dismiss();
}
}
Upvotes: 1
Reputation: 157457
You should add the internet permission to the AndroidManifest.xml
file. Also you should not perform blocking operations on the UI Thread
. In fact the Http execute
will cause the UI Thread
to hang waiting for a response from the server. This will cause ANR
(application not responding). I suggest you to read the following article
Upvotes: 4
Reputation: 393846
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();
Well the null pointer exception happens because you probably get an exception in your client.execute
line and do noting about it (you just catch it). That's why pres
is null. You should print the exception to find your real problem.
Upvotes: 0