API 23 android中的HttpPost给出错误
我的android应用程序中有一个使用HttpPost类的方法。 它在目标sdk 4.4.2上运行良好,但我做了一些更改,将目标sdk修改为23(6.0)。 现在HttpPost类给出了错误。 我也读过HttpUrlConnection,但不知道如何使用它。 这是我的密码API 23 android中的HttpPost给出错误,android,http,http-post,android-6.0-marshmallow,Android,Http,Http Post,Android 6.0 Marshmallow,我的android应用程序中有一个使用HttpPost类的方法。 它在目标sdk 4.4.2上运行良好,但我做了一些更改,将目标sdk修改为23(6.0)。 现在HttpPost类给出了错误。 我也读过HttpUrlConnection,但不知道如何使用它。 这是我的密码 private String getJSON(String URL, JSONObject obj) throws JSONException { String responseString = null;
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;
}
请帮助我可以做什么,或者如果可能的话,为这个函数提供可以在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();
}
}
}
}
你得到了什么错误?我只是得到了无法解析为类型错误的错误。没有任何选项可以导入任何东西,但只要我将目标sdk更改为4.4.2,它就可以正常工作。您可以进行堆栈后跟踪吗?所以它可以更清楚。你能为
build.gradle
添加你的代码吗?我正在使用eclipse IDE,我无法将堆栈跟踪粘贴到这里,因为它没有运行,并且在类HttpPost和HttpParams上显示错误-“无法解析为类型”。我已经尝试过了,但这仍然让我陷入错误block@raviraghav,如果尚未添加这些导入语句,请添加它<代码>导入java.io.BufferedReader;导入java.io.DataOutputStream;导入java.io.InputStreamReader;导入java.net.HttpURLConnection;导入java.net.URL@raviraghav,导入java.io.BufferedReader;导入java.io.DataOutputStream;导入java.io.InputStream;导入java.io.InputStreamReader;导入java.net.HttpURLConnection;导入java.net.URL;导入net.arnx.jsonic.JSONException;导入org.json.JSONObject上面的代码>导入是运行方法所需的所有导入私有字符串getJSON(字符串URL,JSONObject obj)抛出JSONException
。我已经在eclipse中尝试了相同的代码,它工作正常,没有任何编译时错误。相同的代码使我陷入错误块。请将您的工作代码发布到这里。