這裏我寫了代碼來實現HttpPost
在android中的請求,它應該在onClick
方法之後運行。javax.net.ssl.SSLException在Android中使用HttpPost請求
那麼,我沒有得到解決方案,這是我試過的第三個程序,現在正在更新問題。
但我不知道每次我得到相同的error
。請看看這種情況。
我相信,這些代碼幾乎是正確的,任何小的修正,都需要與我在這裏所做的不同。
因爲它可以在eclipse
中作爲簡單的java程序正常工作,但android studio
會拋出exception
。
這是onClick
方法:
public void onClick(View v) {
inputSub = subIn.getText().toString();
String url = "https://some_url";
String inputJson = null;
try {
inputJson = "{ \"asset\": {\"id\": ..........}";
}catch (JSONException e){
e.printStackTrace();
}
new CreateIncident(url, inputJson).execute();
}
這是AsyncTask class
:
private class CreateIncident extends AsyncTask<Void, Void, Void> {
private final String TAG = "HttpClient";
final String user ="some_user";
final String password ="some_pwd";
String serUrl;
String inputJson ="";
public CreateIncident(String serUrl, String inputJson) {
this.serUrl = serUrl;
this.inputJson = inputJson;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(CreateIncidentActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
String authString = user + ":" + password;
byte[] authBytes = authString.getBytes();
String authStringEnc = Base64.encodeToString(authBytes, Base64.DEFAULT);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(serUrl);
postRequest.setHeader("Authorization", "Basic " + authStringEnc);
postRequest.setHeader("Accept", "application/json");
postRequest.setHeader("Content-type", "application/json");
StringEntity input = new StringEntity(inputJson);
input.setContentType("application/json");
postRequest.setEntity(input);
HttpResponse response = httpClient.execute(postRequest);
StringBuilder sb = new StringBuilder();
try {
BufferedReader reader =
new BufferedReader(new InputStreamReader(response.getEntity().getContent()), 65728);
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
}
catch (IOException e) { e.printStackTrace(); }
catch (Exception e) { e.printStackTrace(); }
System.out.println("finalResult " + sb.toString());
System.out.println(response.getStatusLine().getReasonPhrase());
if (response.getStatusLine().getStatusCode() != 201) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatusLine().getStatusCode());
}
BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));
httpClient.getConnectionManager().shutdown();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
}
}
在這裏,我沒有得到在Android Monitor
stackTrace
表現出任何異常,但調試後我發現它拋出異常
javax.net.ssl.SSLException: Read error: ssl=0xd6483780: I/O error during system call, Connection reset by peer
而且對於同一個程序,錯誤代碼有所不同。
我無法找到我在這裏做的錯誤。
請有人幫助我。
Thaks很多。通過添加這一行程序執行。但現在在這一行'HttpResponse response =(HttpResponse)httpclient.execute(httpPostRequest);''response'是_null_。而_error_是通過peer android'重置的javax.net.ssl.sslexception連接 – Shambhu