HttpPost 在 API 23 android 中给出错误
HttpPost giving error in API 23 android
我的 android 应用程序中有一个使用 HttpPost class 的方法。
它在目标 sdk 4.4.2 上运行良好,但我进行了一些更改并将目标 sdk 设为 23(6.0)。
现在 HttpPost class 出错了。
我也读过 HttpUrlConnection 但不知道如何使用它。
这是我的代码
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;
}
请帮助我做些什么,或者如果可能的话,请提供此功能 classes 将在 23 上运行。
提前致谢
使用 HttpUrlConnection
将函数内的代码替换为下面给定的代码
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONException;
import org.json.JSONObject;
public class TestJava {
private String getJSON(String URL, JSONObject obj) throws JSONException {
URL url;
HttpURLConnection connection = null;
try {
//Create connection
url = new URL(URL);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/json");
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setConnectTimeout(30000);
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
wr.write(obj.toString().getBytes("UTF-8"));
wr.flush ();
wr.close ();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if(connection != null) {
connection.disconnect();
}
}
}
}
我的 android 应用程序中有一个使用 HttpPost class 的方法。 它在目标 sdk 4.4.2 上运行良好,但我进行了一些更改并将目标 sdk 设为 23(6.0)。 现在 HttpPost class 出错了。 我也读过 HttpUrlConnection 但不知道如何使用它。 这是我的代码
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;
}
请帮助我做些什么,或者如果可能的话,请提供此功能 classes 将在 23 上运行。 提前致谢
使用 HttpUrlConnection
将函数内的代码替换为下面给定的代码import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONException;
import org.json.JSONObject;
public class TestJava {
private String getJSON(String URL, JSONObject obj) throws JSONException {
URL url;
HttpURLConnection connection = null;
try {
//Create connection
url = new URL(URL);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/json");
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setConnectTimeout(30000);
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
wr.write(obj.toString().getBytes("UTF-8"));
wr.flush ();
wr.close ();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if(connection != null) {
connection.disconnect();
}
}
}
}