我怎么能解决这个问题我无法得到我的InputStream
InputStream inputStream = connection.getInputStream();
功能:
StringBuffer result = new StringBuffer("");
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User_Agent", "");
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
} catch (IOException e) {
e.printStackTrace();
Log.e("REQUEST Result", "IOException = " + e.getMessage());
}
Log.d("REQUEST Result", "result = " + result);
return result;
}
logcat的:
05-04 18:18:31.190 5027-5093/? W/System.err: java.io.EOFException
05-04 18:18:31.194 5027-5093/? W/System.err: at libcore.io.Streams.readAsciiLine(Streams.java:203)
05-04 18:18:31.194 5027-5093/? W/System.err: at libcore.net.http.HttpEngine.readHeaders(HttpEngine.java:621)
05-04 18:18:31.194 5027-5093/? W/System.err: at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:574)
05-04 18:18:31.194 5027-5093/? W/System.err: at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:821)
05-04 18:18:31.194 5027-5093/? W/System.err: at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
05-04 18:18:31.194 5027-5093/? W/System.err: at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
05-04 18:18:31.198 5027-5093/? W/System.err: at com.example.sabrine123.carhapaty.Fragments.AddCarFragment.request(AddCarFragment.java:206)
05-04 18:18:31.198 5027-5093/? W/System.err: at com.example.sabrine123.carhapaty.Fragments.AddCarFragment$AddAnnonce.doInBackground(AddCarFragment.java:154)
05-04 18:18:31.198 5027-5093/? W/System.err: at com.example.sabrine123.carhapaty.Fragments.AddCarFragment$AddAnnonce.doInBackground(AddCarFragment.java:122)
05-04 18:18:31.198 5027-5093/? W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-04 18:18:31.202 5027-5093/? W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-04 18:18:31.202 5027-5093/? W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-04 18:18:31.202 5027-5093/? W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-04 18:18:31.202 5027-5093/? W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-04 18:18:31.202 5027-5093/? W/System.err: at java.lang.Thread.run(Thread.java:856)
05-04 18:18:31.202 5027-5093/? E/REQUEST Result: IOException = null
05-04 18:18:31.206 5027-5093/? D/REQUEST Result: result =
05-04 18:18:31.206 5027-5027/? I/System.out: erreur = End of input at character 0 of
答案 0 :(得分:0)
在尝试从中获取InputStream之前,应检查HttpURLConnection是否为null。
从HttpURLConnection获取输入流时:
InputStream inputStream = new BufferedInputStream(connection.getInputStream());
在您完成connection.connect()
之后,我认为您url.openConnection()
完全没必要这样做;
我还注意到你发布了内容。来自Android开发人员page on HttpURLConnection:
为了获得最佳性能,您应该在事先知道体长时调用setFixedLengthStreamingMode(int),否则调用setChunkedStreamingMode(int)。否则,HttpURLConnection将被强制在传输之前缓冲内存中的完整请求主体,浪费(并可能耗尽)堆并增加延迟。
该页面可能会解决您使用该课程的任何其他问题。