gpt4 book ai didi

java - 来自 Http 客户端的 HttpURLConnection 更新

转载 作者:行者123 更新时间:2023-11-30 01:41:25 28 4
gpt4 key购买 nike

您好,我想知道是否有人可以帮助我解决以下问题,我有一个当前已填充的数据库。我曾经使用 http 客户端调用它并且它工作正常但现在我正在尝试更新代码因为它已被弃用以使用 httpurlconnection 但我没有成功。我查阅了一些教程并尝试了一些方法,但它似乎没有用。数据库通过 php 文件调用并以 json 格式返回。如果我从浏览器调用 php 文件,响应如下:[{"id":"15","logo":"logo url ","标题":"标题"}]

我在控制台上得到的错误如下:java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.InputStream.close()' on a null object reference

这对我来说意义不大,因为脚本会提取信息

我有以下代码,我留下了注释部分以防万一我需要它,它还包括我用来调用数据库的旧方法谢谢!:

public void loadNews(){ InputStream = null;

    String result = "";
ArrayList<NameValuePair>();

try {
URL url = new URL("http://databasecall.php");
//HttpClient httpclient = new DefaultHttpClient();
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//urlConnection.setRequestMethod("GET");
//urlConnection.setRequestProperty("Content-length", "0");
//urlConnection.setUseCaches(false);
//urlConnection.setAllowUserInteraction(false);
//urlConnection.setConnectTimeout(15000);
//urlConnection.setReadTimeout(15000);
//urlConnection.connect();
int responseCode = urlConnection.getResponseCode();
Log.i("Tag:", Integer.toString(responseCode)); //tag 200
//HttpPost httppost = new HttpPost("http://databasecall.php");
//httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//HttpResponse response = httpclient.execute(httppost);
//HttpEntity entity = response.getEntity();
//is = entity.getContent();

/*}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}*/

//convert response to string
//try{
//BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());

BufferedReader reader = new BufferedReader(new InputStreamReader(in));
//BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.i("Tag:", result);
}
}catch(Exception e){
Log.e("log_tag", "Error converting result " + e.toString());
}

最佳答案

更新的 API

try {
String urlParameters = "name=toni&class=one&param3=ok";
byte[] postData = urlParameters.getBytes(Charset.forName("UTF-8"));
int postDataLength = postData.length;
String request = "http://rocks.php";
URL url = new URL(request);
HttpURLConnection cox = (HttpURLConnection) url.openConnection();
cox.setDoOutput(true);
cox.setDoInput(true);
cox.setInstanceFollowRedirects(false);
cox.setRequestMethod("POST");
cox.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
cox.setRequestProperty("charset", "utf-8");
cox.setRequestProperty("Content-Length",
Integer.toString(postDataLength));
cox.setUseCaches(false);
OutputStreamWriter writer = new OutputStreamWriter(
cox.getOutputStream());

writer.write(urlParameters);
writer.flush();
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(
cox.getInputStream()));
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
writer.close();
reader.close();
} catch (Exception e) {

result = e.toString();
Sucess = false;
e.printStackTrace();
}

关于java - 来自 Http 客户端的 HttpURLConnection 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34457080/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com