我编写了一个代码,用于从uhunt.onlinejudge.org读取两个JSON文件。当读取第一个文件(完全读取)时,如果我尝试读取第二个文件,则会抛出java.net.protocolException: Unexpected Status Line
;
我的uHunt课程:
public class uHunt extends AsyncTask<Void, Void ,Void> {
private String userName;
private String uID;
public uHunt(String _uName){
userName = _uName;
}
public uHunt(){}
private void getUID(){
String tempURL = "https://uhunt.onlinejudge.org/api/uname2uid/" + userName;
try {
URL url = new URL(tempURL);
HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
//urlc.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0a2) Gecko/20110613 Firefox/6.0a2");
InputStream is = urlc.getInputStream();
Scanner s = new Scanner(is).useDelimiter("\\A");
String result = s.hasNext() ? s.next() : "";
uID = result;
urlc.disconnect();
is.close();
urlc = null;
is = null;
}
catch (Exception e){
e.printStackTrace();
}
}
protected Void doInBackground(Void ...urls){
JSONObject jsonObject;
try{
getUID();
//uID = "803880";
URL url = new URL("https://uhunt.onlinejudge.org/api/subs-user/" + uID);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Accept-Charset", "UTF-8");
InputStream is = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while( (line = br.readLine()) != null){
sb.append(line);
}
line = sb.toString();
connection.disconnect();
is.close();
sb.delete(0,sb.length());
jsonObject = new JSONObject(line);
}
catch (Exception ex){
Log.d("Exception","Exception thrown");
ex.printStackTrace();
}
return null;
}
}
logcat的:
java.net.ProtocolException: Unexpected status line:
at com.android.okhttp.internal.http.StatusLine.parse(StatusLine.java:54)
at com.android.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:186)
at com.android.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:127)
at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:737)
at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:609)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:471)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:244)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(Unknown Source:0)
at com.example.redwanul.cptracker.uHunt.doInBackground(uHunt.java:69)
at com.example.redwanul.cptracker.uHunt.doInBackground(uHunt.java:23)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
接下来,我从函数doInBackground()中删除了函数调用getUID(),并手动存储了uID以进行检查,这次代码运行时没有任何异常。看起来HttpURLConnector和/或InputStream没有从getUID()方法的调用中正确关闭。但是为了首先获取用户id,我需要先调用getUID()。