我正在尝试创建一个Android应用程序,我在其中调用HTTPS服务器。 我知道如何调用HTTP,但我无法调用HTTPS服务器。我的代码如下所示:
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Spinner spinner;
Button send;
EditText number, comments;
String dropdown;
TextView feedbackText;
private static final String[] paths = {"About Test Center", "About Mobile App"};
final private static String URL = "https://122.180.25.2/TempWS/WebServices/WebService1.asmx";
final private static String METHOD_NAME = "RegisterComplaint";
final private static String NAMESPACE = "https://122.180.25.2/";
final private static String SOAP_ACTION = "https://122.180.25.2/RegisterComplaint";
@Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("Feedback");
send = (Button) findViewById(R.id.button7);
number = (EditText) findViewById(R.id.editText);
comments = (EditText) findViewById(R.id.editText2);
feedbackText = (TextView) findViewById(R.id.textView2);
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, paths);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AsyncFeedback().execute();
}
});
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
switch (position) {
case 0:
dropdown = "About Test Center";
break;
case 1:
dropdown = "About Mobile App";
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
public class AsyncFeedback extends AsyncTask<Void, Void, String> {
String resp;
String Number = number.getText().toString();
String Comments = comments.getText().toString();
@Override
protected String doInBackground(Void... params) {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//request.addProperty("topic",dropdown);
request.addProperty("sender", Number);
request.addProperty("topic", dropdown);
request.addProperty("CompDetails", Comments);
//request.addProperty("details",Comments);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE transportSE = new HttpTransportSE(URL);
transportSE.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject) envelope.bodyIn;
resp = result.toString();
} catch (SocketTimeoutException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return resp;
}
public void onPostExecute(String result) {
dropdown = null;
number.setText("");
comments.setText("");
feedbackText.setText(result);
}
}
}
logcat如下所示:
Click here to view the image of logcat
任何帮助将不胜感激。我想让这段代码工作(如何调用HTTPS)。谢谢!!
答案 0 :(得分:-1)
@ Nair123这是一个基于REST的答案,但这是我用它来信任所有证书的代码片段
X509TrustManager trust;
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[]{
trust = new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
};
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
将所述SSLContext添加到您的SSLSocketFactory,使.sslSocketFactory(sslContext.getSocketFactory(), trust)