Android 4.2.2
我正在解析从PHP服务器发送的JSON字符串。解析相同的字符串每次都会在随机字符编号上出现此异常。 有时它已成功加载。输入的大小为202858字节。我不能在这里发布,因为它是私人数据,但我想它的格式正确。如果我以调试/逐步模式运行我的应用程序,它会一直加载!此外,如果响应的大小较小(行数较少但不确定多少行),它也会一直加载。
以下是我加载流的方式:
String JSONResp = "";
try {
URL u = new URL(params[1]);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setRequestMethod(params[0]);
conn.connect();
/* Here is the new code. This works! */
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"), 4096);
StringBuilder sb = new StringBuilder();
String line = null;
while( (line = br.readLine()) != null ) {
sb.append(line + "\n");
}
JSONResp = sb.toString();
/* Old code starts here. This is not working!
// Read the stream
InputStream is = conn.getInputStream();
byte[] b = new byte[4096];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ( is.read(b) != -1) {
baos.write(b);
}
JSONResp = new String(baos.toByteArray());
*/
JSONArray arr = new JSONArray(JSONResp);
//TODO read result form the input stream
_HTTP_code = 200;
return arr;
}
catch(Throwable t) {
_HTTP_code = ERROR_Throwable;
_HTTP_text = "Error";
_HTTP_body = "Could not parse response!";
Log.e("JSON", "JSONResp.length() = " + JSONResp.length() + ".");
t.printStackTrace();
}
代码是从一个单独的线程执行的,这是我在Android documentation中找到的:
此类的实例不是线程安全的。虽然这个类是非最终的,但它不是为继承而设计的,不应该是子类。特别是,未指定可覆盖方法的自用。有关详细信息,请参阅有效Java项目17,“设计和文档或继承或禁止它”。
我不确定我是否正确理解该文本,但我没有多个线程同时查询服务器。
任何帮助都将不胜感激。
答案 0 :(得分:0)
public class GetResultTask extends AsyncTask<String, Void, String> {
Activity act;
private ProgressDialog pd;
private boolean isInternetConnected = true;
public GetResultTask(Activity _act){
this.act = _act;
pd = ProgressDialog.show(act, null, "Loading...", true );
}
@Override
protected void onPreExecute() {
}
protected void onPostExecute(String result) {
pd.dismiss();
if(!isInternetConnected){
//Toast.makeText(getApplicationContext(), "Check your Network Connection", Toast.LENGTH_LONG).show();
}
}
@Override
protected String doInBackground(String... params) {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("option", "getPeople"));
nameValuePairs.add(new BasicNameValuePair("val", params[0]));
String downloadedString = null;
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/filename.php");
try {
// Execute HTTP Post Request
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8);
httppost.setEntity(ent);
//new
//HttpResponse response = httpClient.execute(httppost);
//System.out.println("Response");
HttpResponse response = httpclient.execute(httppost);
//System.out.println("Response is :-\n"+response);
InputStream in = response.getEntity().getContent();
StringBuilder stringbuilder = new StringBuilder();
BufferedReader bfrd = new BufferedReader(new InputStreamReader(in));
String line;
while((line = bfrd.readLine()) != null)
stringbuilder.append(line);
//string returned as JSON
downloadedString = stringbuilder.toString();
}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch(UnknownHostException e){
isInternetConnected = false;
}
catch (IOException e) {
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
//System.out.println(downloadedString);
return downloadedString;
}
}