从Rails下载到Android时连接重置

时间:2014-01-23 02:29:09

标签: android ruby-on-rails sockets

我有android application我正在尝试与rails app同步。

在Android应用程序中,我从服务器下载并加载了一个json对象(使用OAuth令牌进行身份验证):

private JSONArray getJSON(URL url, String authToken) throws IOException, JSONException {
    URLConnection con = url.openConnection();
    con.setRequestProperty("Content-Type", "application/json");
    con.setRequestProperty("Authorization", "Bearer " + authToken);
    InputStream is = new BufferedInputStream(con.getInputStream());
    Scanner s = new Scanner(is).useDelimiter("\\A");
    String text = s.hasNext() ? s.next() : "";
    return new JSONArray(text);
}

我正在尝试重新加载现有数据,它是~380k。当我运行代码时,我在rails服务器日志中获得以下内容:

Started GET "/events.json?created_since=0" for 192.168.1.111 at 2014-01-22 20:15:16 -0500
Processing by EventsController#index as JSON
  Parameters: {"created_since"=>"0", "event"=>{}}
  Doorkeeper::AccessToken Load (0.6ms)  SELECT "oauth_access_tokens".* FROM "oauth_access_tokens" WHERE "oauth_access_tokens"."token" = [:filtered:] ORDER BY "oauth_access_tokens"."id" ASC LIMIT 1
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 501661262]]
  Habit Load (0.7ms)  SELECT "habits".* FROM "habits" WHERE "habits"."user_id" = ? ORDER BY "habits"."id" ASC LIMIT 1000  [["user_id", 501661262]]
  Event Load (26.7ms)  SELECT "events".* FROM "events" WHERE "events"."habit_id" = ?  [["habit_id", 1]]
  ⋮
  Rendered events/index.json.jbuilder (3422.5ms)
Completed 200 OK in 4491ms (Views: 3436.2ms | ActiveRecord: 73.4ms)
[2014-01-22 20:15:21] ERROR Errno::ECONNRESET: Connection reset by peer
    /home/will/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:80:in `eof?'
    /home/will/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:80:in `run'
    /home/will/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

连接重置重复七次。客户端接收大约260k的数据。 app/views/events/index.json.jbuilder是:

json.array!(@events) do |event|
  json.extract! event, :id, :habit_id, :time, :description
end

使用相同的方法加载只有少量条目的不同模型,并正确加载。文件的下载量有限制吗?在任何情况下,分页看起来都是个好主意。任何人都知道我应该把它分成什么尺寸的任何指导原则?

2 个答案:

答案 0 :(得分:0)

使用扫描仪,也许您可​​以使用Reader来阅读结果InputStream

Reader reader = new InputStreamReader(is);
JsonParser parser = new JsonParser();
JsonArray jsonArray = parser.parse(reader).getAsJsonArray();

答案 1 :(得分:0)

我最终对数据进行了分页,现在可以正确加载:

JSONArray events;
int page = 1;
SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
batch = new ArrayList<ContentProviderOperation>();

do {
    events = getJSON(new URL(EVENT_READ_URL + "?page=" + (page++)), authToken);
    for(int i = 0; i < events.length(); i++) {
        JSONObject event = events.getJSONObject(i);
        batch.add(ContentProviderOperation.newInsert(HabitContentProvider.EVENTS_URI)
         .withValue(EventTable.COLUMN_ID, event.getInt("id"))
         .withValue(EventTable.COLUMN_HABIT_ID, event.getInt(EventTable.COLUMN_HABIT_ID))
         .withValue(EventTable.COLUMN_TIME, timeFormat.parse(event.getString(EventTable.COLUMN_TIME)).getTime() / 1000)
         .withValue(EventTable.COLUMN_DESCRIPTION, event.getString(EventTable.COLUMN_DESCRIPTION))
         .build());
    }
} while(events.length() > 0);

try {
    mContentResolver.applyBatch(HabitContentProvider.AUTHORITY, batch);
} catch(SQLiteConstraintException e) {
    Log.e(TAG, "SQLiteConstraintException: " + e.getMessage());
}

在我的rails代码中,我添加了will_paginate gem,included array support,并将以下内容添加到我的代码中:

if params[:page]
  @events = @events.paginate(page: params[:page], per_page: params[:per_page] || 300)
end