I have a Servlet that sends back a JSON Object and I would like to use this servlet in another Java project. I have this method that gets me the results:
public JSONArray getSQL(String aServletURL)
{
JSONArray toReturn = null;
String returnString = "";
try
{
URL myUrl = new URL(aServletURL);
URLConnection conn = myUrl.openConnection();
conn.setDoOutput(true);
BufferedReader in = new BufferedReader( new InputStreamReader( conn.getInputStream() ) );
String s;
while ((s = in.readLine()) != null )
returnString += s;
in.close();
toReturn = new JSONArray(returnString);
}
catch(Exception e)
{
return new JSONArray();
}
return toReturn;
}
This works pretty will, but the problem I am facing is the following: When I do several simultaneous requests, the results get mixed up and I sometimes get a Response that does not match the request I send.
我怀疑这个问题与我回过来的方式有关: 阅读者阅读了该链接的InputStream的引力。
How can I make sure that I get one reques -> one corresponding reply ? Is there a better way to retrieve my JSON object from my servlet ?
Cheers, Tim