使用android https时出错

时间:2014-01-09 15:57:41

标签: android

我正在尝试使用我的php服务器登录我的应用程序..当我的服务器网址类似于 http://www.myservername.com/login.php 但当我的服务器地址以https:// like https://www.myservername.com/login.php 然后出现问题,它给我异常

  

javax.net.ssl.sslpeerunverifiedexception no peer certificate

我的代码如下。

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

请帮助...

1 个答案:

答案 0 :(得分:0)

这是服务器上的SSL证书设置错误。你的代码运行正常。一个月前我遇到了同样的问题。查看Safely fixing: javax.net.ssl.SSLPeerUnverifiedException: No peer certificate了解更多信息。同时搜索SO以查找类似的问题。

修改

如果您认为SSL证书是正确的,您可以禁用证书的Android检查,但这是不赞成的。更多:Https Connection Android

如果您对此实施有困难,droidQuery提供了一种实现(代码here)。您还可以使用droidQuery AjaxOption trustAllSSLCertificates来使用库本身。有关详情,请参阅