0

I am trying to login my application using my php server ..it works fine when my server url is like http://www.myservername.com/login.php but when my server address starts with https:// like https://www.myservername.com/login.php then problem arise it giving me exception

javax.net.ssl.sslpeerunverifiedexception no peer certificate

my code is below.

public class Main extends Activity {
public static DefaultHttpClient client;
EditText useremail;
EditText password;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


Button login_button= (Button) findViewById(R.id.login_button);
login_button.setOnClickListener(new View.OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
try{    
useremail=(EditText) findViewById(R.id.useremail);  
String useremail_string=useremail.getText().toString();
password=(EditText) findViewById(R.id.password);    
String password_string=password.getText().toString();

if("".equals(useremail_string) || "".equals(password_string)){
Toast.makeText(getApplicationContext(), "Empty field detected", Toast.LENGTH_SHORT).show(); 
}//empty check

/***Login process begin***/
else{
String s="";
String d="";
try {
StrictMode.enableDefaults();                    
JSONObject json = new JSONObject();
json.put("username", useremail_string);
json.put("password", password_string);
client = new DefaultHttpClient();
String url = "https://www.myservername.com/mobile_app/login/check_user";
HttpPost request = new HttpPost(url);
request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
request.setHeader("json", json.toString());
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null){
InputStream instream = entity.getContent();
String result="";
try{
BufferedReader reader= new BufferedReader(new InputStreamReader(instream,"iso-8859-1"),8);  
StringBuilder sb=new StringBuilder();
String line=null;
while((line=reader.readLine())!=null){
sb.append(line+"\n"); }
instream.close();
result= sb.toString();
}catch(Exception e){}
JSONArray jArray= new JSONArray(result);
JSONObject getjson=jArray.getJSONObject(0);
s=getjson.getString("message");
d=getjson.getString("data");
if("success".equals(s))
{   
password.setText(null);  
SharedPreferences share= getSharedPreferences("Userdata", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=share.edit();
editor.putString("session_value", d);
editor.commit();

Intent profile=new Intent(Main.this,BidActivity.class);
profile.putExtra(EXTRA_MESSAGE, d);
startActivity(profile);
}
else
{
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();

}}    
}catch(Throwable t){}
}
/***Login process end***/
}catch(Exception e){  } 
}}); 
}   
}// end of function onCreate

}//end of class

please help guys...

Brett
  • 431
  • 2
  • 10
  • 25

1 Answers1

0

This is an error with the SSL Certificate setup on the server. Your code works fine. I ran into this same issue about a month ago. Check out Safely fixing: javax.net.ssl.SSLPeerUnverifiedException: No peer certificate for more. Also search SO for similar problems.

Edit

If you believe the SSL Certificate is correct, you can disable the Android check of the certificate, but this is frowned upon. More: Https Connection Android

If you have difficulty with this implementation, droidQuery provides an implementation (code here). You can also use the droidQuery AjaxOption trustAllSSLCertificates to use the library itself. For more on this, see .

Community
  • 1
  • 1
Phil
  • 35,089
  • 23
  • 123
  • 159
  • This is not an answer, but rather a comment I would say – Lefteris Jan 09 '14 at 16:04
  • thank you @Phil .. can I overcome this exception anyhow from my android application...?? – Brett Jan 09 '14 at 16:06
  • @Brett, you can only overcome it by using `http`. If you want SSL, it must be fixed on the server. – Phil Jan 09 '14 at 16:09
  • https://www.ssllabs.com/ssltest/analyze.html I am using this link to check my domain name ssl...it says it is fully trusted... – Brett Jan 09 '14 at 16:11
  • @Lefteris this answers the question, which is not the point of a *comment*. – Phil Jan 09 '14 at 16:11
  • I am seeing your link http://stackoverflow.com/questions/10359140/android-ignoring-self-signed-cert-errors-actual-implementation but it getting error in FakeSocketFactory() it shows an error like FakeSocketFactory can't be resolved to a type..and what does mean ABCApplication here.. – Brett Jan 09 '14 at 16:22
  • @Brett I added a paragraph about [droidQuery](http://bit.ly/droidquery), which is a library I wrote that uses this feature. The referenced code should help, or you can use the library. – Phil Jan 09 '14 at 16:27