我在我的Android應用程序中使用HttpPost類的方法。 它與目標sdk 4.4.2正常工作,但我做了一些改變,並使目標sdk到23(6.0)。 現在HttpPost類發生錯誤。 我也讀過關於HttpUrlConnection,但不知道如何使用它。 這裏是我的代碼HttpPost在API 23中給出錯誤android
private String getJSON(String URL, JSONObject obj) throws JSONException {
String responseString = null;
try {
HttpPost httppost = new HttpPost(URL);
StringEntity se = new StringEntity(obj.toString(), HTTP.UTF_8);
httppost.setHeader("Content-Type", "application/json");
httppost.setEntity(se);
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
HttpClient httpclient = new DefaultHttpClient(httpParams);
HttpResponse httpResponse = httpclient.execute(httppost);
StatusLine statusLine = httpResponse.getStatusLine();
int statusCode = statusLine.getStatusCode();
System.out.println("status code is" + statusCode);
HttpEntity entity = httpResponse.getEntity();
responseString = EntityUtils.toString(entity, "UTF-8");
System.out.println("response string" + responseString);
Log.i("RESPONSE XML ------> ", "-----> " + responseString);
return responseString;
} catch (Exception e) {
e.printStackTrace();
}
return responseString;
}
請幫助我能做些什麼,或者如果可能提供的類此功能,將提前
你得到什麼錯誤? – Exception
我剛剛得到不能解決類型錯誤。並且沒有選擇導入任何東西,但只要我將目標sdk更改爲4.4.2就可以正常工作。 –
你可以發佈堆棧跟蹤嗎?所以它可以更清楚。 – Talha