我在我的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